簡體   English   中英

如何打開存儲在Android中Assets下的文件夾中的圖像

[英]How to open an image that is stored in a folder under Assets in android

我在Android Studio中的Build,Libs和Src的App下創建了一個Assets文件夾目錄,在其中分別放置了帶有圖像的文件夾。 我有一個問題,找不到包含該文件名或路徑的文件,但我知道它的正確性,請告訴我在遇到問題時該怎么做。 該文件位於此目錄Assets / profileicon / 26.png中,該數字由profileIconTag(在本例中為26)確定,我是否為.open()設置了正確的路徑名?

AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open("/profileicon/" + profileIconTag + ".png");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    profileIcon.setImageBitmap(bitmap);

檢查資產文件夾是否在正確的路徑中。

InputStream bitmap=null;
try {
    bitmap=getAssets().open(profileIconTag +".png");
    Bitmap bit=BitmapFactory.decodeStream(bitmap);
    profileIcon.setImageBitmap(bit);
} catch (IOException e) {
    e.printStackTrace();
} finally {
   try{
    if(bitmap!=null)
    bitmap.close();
   }catch(Exception ex) {}
}

您可以使用AssetManageropen()方法獲取InputStream ,然后使用BitmapFactory的decodeStream()方法獲取Bitmap。 檢查此鏈接

private Bitmap getBitmapFromAsset(String strName)
{
    AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open(strName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM