2011-10-04 16 views
27

いくつかの特定のケースでは、froyo 2.2.2のバグ修正POTイメージを避けるために、資産からイメージをロードする必要があります。これを避ける方法は、アセットdirからイメージファイルをロードすることです。私はこれをやろうとしているアセットから画像を読み込む方法は?

 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) { 
     } 
    } 

しかし、私はFileNotFoundExceptionを取り込むis.close();

行に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を/にファイルを入れて、あなたがして、次のURIを使用してアクセスすることができている:android.resource://com.your.packagename/" + R.raw.radiocd5

+1

の関連部分を追加しましたこのアプリケーションは私たちの会社ジェネレータで自動的に生成され、dirsを作成することはできませんので、作成してください – 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; 
    } 
関連する問題