Load bitmap from SD card don't work

自作多情 提交于 2019-12-12 04:14:00

问题


I have some problem show images from sd card, i try to load bitmap into imageview.

This is the code to load image:

    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());
        }

load bitmap into image view:

    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();
    }

and here show the result:

and i have this log result:


回答1:


Try this inside your code:

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

Example

File Read Permission:

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


来源:https://stackoverflow.com/questions/24246972/load-bitmap-from-sd-card-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!