从外部存储 Android 读取图像/文件 [英] Read an Image/file from External storage Android

查看:35
本文介绍了从外部存储 Android 读取图像/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从外部存储加载图像.我设置了权限,尝试了不同的方法,但没有一个有效.

I'm trying to load an image from external storage. I set the permissions, I tried different ways, but none of them works.

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

    Bitmap bitmap = BitmapFactory.decodeFile(file.toString()); 

    tv.setImageBitmap(bitmap);

还有这个,

FileInputStream streamIn = new FileInputStream(file);
Bitmap bitmap = BitmapFactory.decodeStream(streamIn); 

    tv.setImageBitmap(bitmap);
        streamIn.close();

推荐答案

如果我在 sdcard 上有文件 abc.jpg 那么:

If i have file abc.jpg on the sdcard then:

String photoPath = Environment.getExternalStorageDirectory() + "/abc.jpg";

并获取位图.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);

Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);

为了避免内存不足错误我建议你使用下面的代码...

to avoide out of memory error I suggest you use the below code...

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile(photoPath, options);

<小时>

为避免上述问题,您可以使用 Picasso(适用于 Android 的强大图像下载和缓存库)


To avoid above issue you can use Picasso (A powerful image downloading and caching library for Android)

文档

怎么做?

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);

这篇关于从外部存储 Android 读取图像/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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