无法从SD卡加载位图 [英] Load bitmap from SD card don't work

查看:109
本文介绍了无法从SD卡加载位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示SD卡中的图像时遇到一些问题,我尝试将位图加载到imageview中.

I have some problem show images from sd card, i try to load bitmap into 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());
        }

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

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

在这里显示结果:

我有这个日志结果:

推荐答案

在您的代码中尝试以下操作:

Try this inside your code:

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

示例

文件读取权限:

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

这篇关于无法从SD卡加载位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆