1

我正在尝试在 android 中显示图像。我使用名为“ image ”的图像视图制作了一个新布局。有问题的字符串是“ SelectedImage =/storage/sdcard0/DCIM/Camera/IMG_20130602_133933.jpg”,代码如下所示:

ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    Bitmap bm = BitmapFactory.decodeFile(SelectedImage, options);
    imageView1.setImageBitmap(bm); 
setContentView(R.layout.image);

当我运行它时它崩溃了。日志说

06-08 21:24:41.192: I/System.out(7642): Not a DRM File, opening notmally
06-08 21:24:41.192: I/System.out(7642): buffer returned 
06-08 21:24:41.232: D/AndroidRuntime(7642): Shutting down VM
06-08 21:24:41.232: W/dalvikvm(7642): threadid=1: thread exiting with uncaught exception (group=0x414c1438)
4

2 回答 2

1

确保您在清单中设置了适当的权限并尝试以下操作:

// This is the layout file that includes the imageview
setContentView(R.layout.image);

String selectedImage = Environment.getExternalStorageDirectory() + "/DCIM/Camera/IMG_20130602_133933.jpg";
File f_path = new File(selectedImage);
InputStream fis = new FileInputStream(f_path);

ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeStream (fis, null, options)
imageView1.setImageBitmap(bm); 
于 2013-06-09T01:56:52.240 回答
1

首先检查是否将权限添加到Android清单文件

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-06-09T03:12:42.710 回答