2016-11-05 62 views
-3

我尝试从图库中显示位图。这是我的网址无法在ImageView中显示位图

file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg 

我知道怎么去使用onActivityResult()位图,但我不知道如何让一个位图

这是我的源

final ImageView imageView=(ImageView)findViewById(R.id.imageView); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 

     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     final Bitmap bitmap = BitmapFactory.decodeFile("file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg", options); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       imageView.setImageBitmap(bitmap); 
      } 
     }); 

我该如何解决我的问题?

+2

的可能的复制[如何显示在Android的位图图像](http://stackoverflow.com/questions/4181774/show-image-view-from-file -path) – NilayDani

+1

从来没有硬编码文件路径在android –

回答

0

请参考下面的代码

BitmapFactory.Options options = new BitmapFactory.Options(); 

      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      //Get our saved file into a bitmap object: 
      File file = new File(Environment.getExternalStorageDirectory()+File.separator + "DCIM"+ File.separator + "Camera "+File.separator + "IMG_20161103_180603.jpg"); 
      Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); 
      imageView.setImageBitmap(bitmap); 
+0

我试过了,但位图为空 – BekaKK

+0

你可以请检查文件是否存在于给定的路径或不 –

0

我不得不引起其无法在运行时处理大量的图像尺寸相同的问题。用这个解决它。

// Decodes image and scales it to reduce memory consumption 
public static Bitmap decodeFile(File f) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f), null, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 200; 

     // Find the correct scale value. It should be the power of 2. 
     int scale = 1; 
     while (o.outWidth/scale/2 >= REQUIRED_SIZE && 
       o.outHeight/scale/2 >= REQUIRED_SIZE) { 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

其中File˚F将使用https://stackoverflow.com/a/20559418/3758972

只是把你想在REQUIRED_SIZE(通常是您的ImageView的大小)缩略图的大小进行检索和你做。它会给你一个缩放的图像,你可以设置为你的imageview

0
decodeFile("file:///storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg 

更改为:

decodeFile("/storage/emulated/0/DCIM/Camera/IMG_20161103_180603.jpg