且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-22 22:23:22

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