将下载的文件保存到内部存储器java/android失败

问题描述:

我有一个应用程序必须下载一些生成的图像(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() + "/" +)。不出所料,它没有任何区别。

图像没问题,我可以在浏览器中看到它们。还尝试使用上传的图像 - 而不是生成的图像,结果相同。

这很奇怪,有没有人有任何想法?

谢谢!

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(); 
    } 

希望这个功能可以帮助你。如果这无助于随意问