2017-04-27 46 views

回答

0

使用Picasso您需要將您的drawable/bitmap轉換爲文件。 Picasso可以加載圖像文件。

要將位圖/繪圖文件轉換爲文件,請使用此選項。只需將參數從位圖更改爲可繪製。

public static File bitmapToFile(Bitmap bitmap, String fileName) { 
    try { 
     File file = File.createTempFile(fileName, ".png"); 
     if (file.exists()) { 
      file.delete(); 
     } 
     file.createNewFile(); 
     file.deleteOnExit(); 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); 
     byte[] bitmapData = bos.toByteArray(); 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(bitmapData); 
     fos.flush(); 
     fos.close(); 
     return file; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

並加載,你可以這樣做:

File bitmapFile = bitmapToFile(yourBitmap, "aRandomName"); 
Picasso.with(context).load(bitmapFile).into(imageView); 
+0

這是我的轉換後的代碼,但如何.. Drawable icon = p.applicationInfo.loadIcon(getPackageManager()); 位圖位圖=((BitmapDrawable)圖標).getBitmap(); – Rakhiii

+0

請給我示例代碼... – Rakhiii

+0

@Rakhiii你在這裏。 –

1

你可以這樣寫:

Picasso.with(上下文) .load(R.drawable.yourDrawable) .into(ImageView的);在

更多信息:http://square.github.io/picasso/

希望它能幫助。

+0

如果它解決了您的問題,請將其標記爲正確答案。謝謝。 –

+0

可繪製對象(不來自資源) – Rakhiii

0

您可以使用它的url或resourceId或uri加載圖像。不能用畢加索

Picasso.with(getContext()).load(imageResourceId) 
.into(imageView); 

直接添加繪製到ImageView的,如果你要調整你的繪製或位圖文件,搜索

如何加載位圖有效

+0

可繪製對象(不來自資源) – Rakhiii

相關問題