简体   繁体   中英

Error while saving bitmap to sdcard in android

I have a problem that when I am going to save my bitmap image to sdcard in PNG format then code always returns an error which is described below, I don't know why? Please suggest me the right solution regarding the same.

Error Stack:

12-28 12:50:58.765: ERROR/AndroidRuntime(16977): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.greetingApp.common/com.greetingApp.common.Greeting}: java.lang.NullPointerException
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.os.Looper.loop(Looper.java:123)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at java.lang.reflect.Method.invokeNative(Native Method)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at java.lang.reflect.Method.invoke(Method.java:521)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at dalvik.system.NativeStart.main(Native Method)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977): Caused by: java.lang.NullPointerException
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at com.greetingApp.common.Greeting.SaveData(Greeting.java:138)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at com.greetingApp.common.Greeting.onCreate(Greeting.java:78)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-28 12:50:58.765: ERROR/AndroidRuntime(16977):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

Code:

protected void SaveData() 
        {


          FrameLayout view = (FrameLayout)findViewById(R.id.frameLayout2);
          view.setDrawingCacheEnabled(true);
          view.buildDrawingCache();
          bm = view.getDrawingCache();

          OutputStream outStream = null;
           File file = new File(extStorageDirectory, "er.PNG");
           try {
            outStream = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();

            //Toast.makeText(AndroidWebImage.this, "Saved", Toast.LENGTH_LONG).show();

           } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            //Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           // Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();

            // TODO Auto-generated method stub

        }

Thanks in advance

Make sure you don't have null here

 FrameLayout view = (FrameLayout)findViewById(R.id.frameLayout2);

Also its better to fetch the id inside the onCreate and utilize it in the SaveData()

FrameLayout view;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_xml);
        view = (FrameLayout)findViewById(R.id.frameLayout2);
}

Now you can use this view instance of FrameLayout in your SaveData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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