我正在尝试在 android 中显示图像。我用名为“image”的 ImageView 创建了一个新布局。有问题的字符串是“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)
最佳答案
确保您在 list 中设置了适当的权限并尝试如下操作:
// 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);
关于java - 在字符串中显示带有图像位置的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005608/