阅读从外部存储Android的图像/文件图像、文件、Android

2023-09-08 08:41:55 作者:笑看浮华。苍生

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

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

解决方案 linux开发读取外部存储,Android 外部存储

如果我在文件 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);

 
精彩推荐
图片推荐