简体   繁体   English

从中下载图像时应用程序崩溃

[英]App crashing on downloading the Image from it

While Clicking on download button app is crashing and showing java.lang.NullPointerException单击下载按钮应用程序崩溃并显示 java.lang.NullPointerException

at com.$$$$$$.fieldforce.adapter.InstallDocAdapter.saveToGallery在 com.$$$$$$.fieldforce.adapter.InstallDocAdapter.saveToGallery

Here is the logcat这是日志

Exception java.lang.NullPointerException:
  at android.graphics.Bitmap.compress (Bitmap.java:1434)
  at com.$$$$$$.fieldforce.adapter.InstallDocAdapter.saveToGallery (InstallDocAdapter.java:123)
  at com.$$$$$$.fieldforce.adapter.InstallDocAdapter.access$100 (InstallDocAdapter.java:40)
  at com.$$$$$$.fieldforce.adapter.InstallDocAdapter$1.onClick (InstallDocAdapter.java:75)
  at android.view.View.performClick (View.java:7488)
  at android.view.View.performClickInternal (View.java:7464)
  at android.view.View.access$3700 (View.java:841)
  at android.view.View$PerformClick.run (View.java:28911)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:233)
  at android.os.Looper.loop (Looper.java:344)
  at android.app.ActivityThread.main (ActivityThread.java:8212)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:584)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1034)

Here is the function used in it这是其中使用的function

private void saveToGallery(String attachment){
    byte [] encodeByte = Base64.decode(attachment,Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);

    FileOutputStream outputStream = null;
    File file = Environment.getExternalStorageDirectory();
    File dir = new File(file.getAbsolutePath() + "/$$$$$$ Downloads");
    dir.mkdirs();

    String filename = String.format("%d.png",System.currentTimeMillis());
    File outFile = new File(dir,filename);
    try{
        outputStream = new FileOutputStream(outFile);
    }catch (Exception e){
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
    try{
        outputStream.flush();
    }catch (Exception e){
        e.printStackTrace();
    }
    try{
        outputStream.close();
        Toast.makeText(context,"Downloaded image ....",Toast.LENGTH_LONG).show();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

Here is the screenshot of the above code这是上面代码的截图

Exception java.lang.NullPointerException:
  at android.graphics.Bitmap.compress (Bitmap.java:1434)
  at com.$$$$$$.fieldforce.adapter.InstallDocAdapter.saveToGallery (InstallDocAdapter.java:123)

You're getting this because saveToGallery calls Bitmap.compress at line 123 and passes null as a parameter that shouldn't be null. I'm assuming it's this line:你得到这个是因为saveToGallery第 123 行调用Bitmap.compress并将null作为不应为 null 的参数传递。我假设它是这一行:

bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);

The first two parameters are fine so it'll be outputStream that's null. Which means this:前两个参数没问题,所以输出流是outputStream这意味着:

try{
    outputStream = new FileOutputStream(outFile);
}catch (Exception e){
    e.printStackTrace();
}

is failing because an exception is being thrown, so outputStream (which is initialised as null ) isn't being assigned.由于抛出异常而失败,因此未分配outputStream (初始化为null )。

The docs for FileOutputStream explain which exceptions are thrown under what circumstances, and you're printing the stacktrace to your logs in the catch block, so find out why it's failing. FileOutputStream的文档解释了在什么情况下会抛出哪些异常,并且您正在将堆栈跟踪打印到 catch 块中的日志中,因此找出它失败的原因。 If you've made a mistake, fix it - if it's a valid scenario that can happen , then you need to handle that possibility in your code.如果您犯了一个错误,请修复它 - 如果这是一个可能发生的有效场景,那么您需要在您的代码中处理这种可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM