2010-10-06 65 views
12

您能幫助我嗎?我已經試過:如何在Android上使用setImageUri()

ImageButton imgbt=(ImageButton)findViewById(R.id.imgbutton); 
Uri imgUri=Uri.parse("/data/data/MYFOLDER/myimage.png"); 
imgbt.setImageUri(imgUri); 

,但我什麼也沒看見,只是一個空白按鈕。

+0

你需要從磁盤上的位置(如SD卡)具體加載圖像或加載它作爲R的資源罰款嗎? – 2010-10-06 08:29:19

+0

也許你需要預先'文件://' – 2015-08-21 12:05:10

回答

6

應該

Uri imgUri=Uri.parse("file:///data/data/MYFOLDER/myimage.png");

+0

thanx,但我也試過這個,仍然沒有:( – ilredelweb 2010-10-06 09:16:35

13
private Bitmap getImageBitmap(String url) { 
     Bitmap bm = null; 
     try { 
      URL aURL = new URL(url); 
      URLConnection conn = aURL.openConnection(); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      bm = BitmapFactory.decodeStream(bis); 
      bis.close(); 
      is.close(); 
     } catch (IOException e) { 
      Log.e(TAG, "Error getting bitmap", e); 
     } 
     return bm; 
    } 
+0

謝謝,這個'getImageBitmap'方法爲我工作。遠遠好於使用'setImageURI' – Solostaran14 2012-11-06 22:07:56

+0

@iredelweb應該真的批准這個回答。 – Max 2015-08-21 12:16:41

3

這個怎麼樣:

Bitmap bitmap = BitmapFactory.decodeFile(fullFileName); 
imgProfileImage.setImageBitmap(bitmap); 
1

其最好的行動,以避免手工的路徑,嘗試:

imgbt.setImageUri(Uri.fromFile(new File("/data/data/...."))); 
0
String imgPath = Environment.getDataDirectory() + "/data/com.wariyum.signage/files/"+ "221215-085656.619.72.jpg"; 

    //Following lines was expected work, but don't really show up image always - no idea why 
    //imgZoom.setImageURI(Uri.parse(imgPath)); 

    //following works always perfectly 
      File imgFile = new File(imgPath); 
      if(imgFile.exists()){ 
       Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
       imgZoom.setImageBitmap(myBitmap); 
      } 
0

我用框架解決它:

  1. 加入gradlle:

    implementation 'com.facebook.fresco:fresco:1.8.0' 
    
  2. 初始化庫中的應用程序級:在XML

    Fresco.initialize(applicationContext) 
    

另外:

<com.facebook.drawee.view.SimpleDraweeView 
    android:id="@+id/avatar" 
    android:layout_width="110dp" 
    android:layout_height="110dp" /> 

並且在代碼中:

binding.avatar.setImageURI(user.getAvatarUrl()) 

對不起,對於選項卡。在網站上的錯誤