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

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

问题描述

我试图加载从外部存储的图像。我设置的权限,我尝试不同的方法,但他们没有工作。

  BitmapFactory.Options选项=新BitmapFactory.Options();
    options.in preferredConfig = Bitmap.Config.ARGB_8888;

    点阵位图= BitmapFactory.de codeFILE(file.toString());

    tv.setImageBitmap(位);
 

而这其中,

 的FileInputStream streamIn =新的FileInputStream(文件);
点阵位图= BitmapFactory.de codeStream(streamIn);

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

解决方案

如果我在文件 abc.jpg SD卡然后

 字符串photoPath = Environment.getExternalStorageDirectory()+/ abc.jpg;
 

和取得位图

  BitmapFactory.Options选项=新BitmapFactory.Options();
options.in preferredConfig = Bitmap.Config.ARGB_8888;
点阵位图= BitmapFactory.de codeFILE(photoPath,期权);
 

 位图bitmap1 = BitmapFactory.de codeFILE(photoPath);
 

以avoide内存不足的错误我会saggest你做下面code ...

  BitmapFactory.Options选项=新BitmapFactory.Options();
options.inSampleSize = 8;
最后的位图B = BitmapFactory.de codeFILE(photoPath,期权);
 

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

and this one,

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

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

解决方案

if i have file abc.jpg in sdcard then

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

and get bitmap.

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

or

Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);

to avoide out of memory error i 'll saggest you do below code...

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

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

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