2011-10-04 108 views
27

我需要從資產中加載圖像以避免在某些特定情況下調整POT圖像的froyo 2.2.2錯誤。避免它的方法是從資產目錄加載圖像文件。如何從資產中加載圖片?

我想這個做:

 String imagePath = "radiocd5.png"; 
    AssetManager mngr = context.getAssets(); 
    // Create an input stream to read from the asset folder 
    InputStream is=null; 
    try { 
     is = mngr.open(imagePath); 
    } catch (IOException e1) { e1.printStackTrace();} 

    //Get the texture from the Android resource directory 
    //InputStream is = context.getResources().openRawResource(R.drawable.radiocd5); 
    Bitmap bitmap = null; 
    try { 
     //BitmapFactory is an Android graphics utility for images 
     bitmap = BitmapFactory.decodeStream(is); 

    } finally { 
     //Always clear and close 
     try { 
      is.close(); 
      is = null; 
     } catch (IOException e) { 
     } 
    } 

但我就行了is.close();

我捕捉FileNotFoundException異常得到NullPointerException異常:radiocd5.png,但該文件是在我的資產目錄:S

我在做什麼壞事?該文件稱爲radiocd5.png,它是在assets目錄而不是使用資產迪爾

回答

51

你可以按照我的教程顯示來自資產的數據:https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
該示例加載要顯示的圖像和文本。

我現在又增加所提供的鏈接 的相關部分(在地震或某事的情況下);-) TAIFUN

// load image 
try { 
    // get input stream 
    InputStream ims = getAssets().open("avatar.jpg"); 
    // load image as Drawable 
    Drawable d = Drawable.createFromStream(ims, null); 
    // set image to ImageView 
    mImage.setImageDrawable(d); 
} 
catch(IOException ex) { 
    return; 
} 
+0

我在做你的教程,爲什麼我會收到錯誤? – NullPointerException

+0

什麼錯誤?請清楚,只要您提供給我足夠的信息,我會盡力幫助您。 –

+0

我編輯我的問題,它給我錯誤is.close();行 – NullPointerException

3

,將文件放入/ RES /生,然後你可以使用訪問以下網址:android.resource://com.your.packagename/" + R.raw.radiocd5

+1

我沒有原始目錄,我不能創建它,因爲這個應用程序將與我們公司的發電機自動生成,並且不能創建目錄 – NullPointerException

2
try { 
     InputStream istr =this.context.getAssets().open(P.strImage); 
     //set drawable from stream 
     this.imgProduct.setImageDrawable(Drawable.createFromStream(istr, null)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
1
protected String openImageInAssets(String imageName){ 
     String encodedImageBase64 = ""; 
     AssetManager assetManager = getAssets(); 
     InputStream fileStream = null; 
     try { 
      fileStream = assetManager.open(imageName); 
      if(fileStream != null){ 
       //     BitmapFactory.Options bfo = new BitmapFactory.Options(); 
       //     bfo.inPreferredConfig = Bitmap.Config.ARGB_8888; 
       //     Bitmap bitmap = BitmapFactory.decodeStream(fileStream, null, bfo); 

       Bitmap bitmap = BitmapFactory.decodeStream(fileStream); 
       // Convert bitmap to Base64 encoded image for web 
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

       // to get image extension file name split the received 
       int fileExtensionPosition = imageName.lastIndexOf('.'); 
       String fileExtension = imageName.substring(fileExtensionPosition+1); 
       //     Log.d(IConstants.TAG,"fileExtension: " + fileExtension); 

       if(fileExtension.equalsIgnoreCase("png")){ 
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); 
        //      Log.d(IConstants.TAG,"fileExtension is PNG"); 
       }else if(fileExtension.equalsIgnoreCase("jpg") || fileExtension.equalsIgnoreCase("jpeg")){ 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); 
        //      Log.d(TAG,"fileExtension is JPG"); 
       } 

       byte[] byteArray = byteArrayOutputStream.toByteArray(); 
       String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT); 
       encodedImageBase64 = "data:image/png;base64," + imgageBase64; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return encodedImageBase64=""; 
     } 
     finally { 
      //Always clear and close 
      try { 
       if(fileStream != null) { 
        fileStream.close(); 
        fileStream = null; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

//  Log.d(TAG,"encodedImageBase64: " + encodedImageBase64); 
     return encodedImageBase64; 
    }