从SD卡加载位图不起作用

时间:2014-06-16 15:17:39

标签: android opengl-es bitmap

我有一些问题从SD卡显示图像,我尝试将位图加载到imageview。

这是加载图片的代码:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

File imagen1 = new File(ruta + contratoActual.getImgDoc1());
    if (imagen1.exists()) {
        try {

        bmap = BitmapFactory.decodeStream(new FileInputStream(imagen1), null, options);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        decoded[0] = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

        } catch (FileNotFoundException e) {
        Log.e("ERROR", e.getMessage());
        }

将位图加载到图像视图中:

    LayoutParams p = (LayoutParams) img1.getLayoutParams();
    p.width = 250;
    p.height = 190;

    if (result[0] != null) {
    img1.setImageBitmap(result[0]);
    img1.setScaleType(ImageView.ScaleType.CENTER_CROP);
    img1.setLayoutParams(p);
    result[0].recycle();
    }

在这里显示结果:

black image

我有这个日志结果:

error

1 个答案:

答案 0 :(得分:0)

在您的代码中尝试此操作:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap YourBitmap = BitmapFactory.decodeFile(YourImagePath, options);

Example

文件读取权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />