2016-11-05 49 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