2017-02-22 22 views
0

生成された画像(PNG)をダウンロードしなければならないアプリがあります。ダウンロードしたファイルを内部ストレージに保存するjava/androidに失敗する

ImageDownloader extends AsyncTaskを試しましたが、doInBackground()がイメージを取得し、onPostExecute()はそれを内部イメージに保存しようとします。

public class HandleImages extends AppCompatActivity { 
String filename = ""; 

public boolean saveImageToInternalStorage(Bitmap image) { 
    try { 
     FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE); 

     image.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.close(); 

     return true; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

public Bitmap retrieveImage(String url){ 
    ImageDownloader task = new ImageDownloader(); 
    Bitmap image = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); 

    try { 
     image = task.execute(url).get(); 
    } catch (InterruptedException e) { 
     MainActivity.debb("InterruptedException - " + e.getMessage() + " in " + new Object(){}.getClass().getEnclosingMethod().getName()); 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     MainActivity.debb("ExecutionException - " + e.getMessage() + " in " + new Object(){}.getClass().getEnclosingMethod().getName()); 
     e.printStackTrace(); 
    } 
    return image; 
} 


public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(String... urls) { 
     try { 
      String[] filenames = urls[0].split("/"); 
      filename = filenames[filenames.length-1] + ".jpg"; 

      URL url = new URL(urls[0]); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

      connection.connect(); 
      InputStream inputStream = connection.getInputStream(); 

      return BitmapFactory.decodeStream(inputStream); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     super.onPostExecute(bitmap); 
     if (bitmap != null) 
      saveImageToInternalStorage(bitmap); 
    } 
} 
} 

と私が得るエラーは以下のとおりです:コード

(の一部)は以下の通りですjava.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference

FileOutputStream fos = openFileOutput(..)は失敗したようですが、その理由はわかりません。

また、ファイル名にパス(sdCard.getPath() + "/" +)を追加しようとしました。予想どおり、何の違いもありませんでした。

画像は正常です。ブラウザで確認できます。また、アップロードされた画像で試した - 生成されたものの代わりに、同じ結果。

これはかなり奇妙なことですが、誰もが考えていますか?

ありがとうございます!

答えて

0
private String saveToInternalStorage(Bitmap bitmapImage){ 
     ContextWrapper cw = new ContextWrapper(getApplicationContext()); 
     // path to /data/data/yourapp/app_data/imageDir 
     File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
     // Create imageDir 
     File mypath=new File(directory,"profile.jpg"); 

     FileOutputStream fos = null; 
     try {   
      fos = new FileOutputStream(mypath); 
     // Use the compress method on the BitMap object to write image to the OutputStream 
      bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     } catch (Exception e) { 
       e.printStackTrace(); 
     } finally { 
      try { 
       fos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return directory.getAbsolutePath(); 
    } 

この機能が役立ちます。これでお気軽にお問い合わせいただけない場合

関連する問題