获得空指针异常而图像保存到SD卡 [英] Getting Null pointer exception while saving the image to sd card

查看:180
本文介绍了获得空指针异常而图像保存到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存的图片插入SD card.But仅命名的图像文件的文件夹在SD卡被创建,但不是在SD card.I正在创建的实际文件我得到空指针异常,而试图该图像保存到SD card.This是code:

I want to save an image into sd card.But only the folder naming the image file is created in the sd card,but the actual file is not being created in the sd card.I am getting null pointer exception while trying to save that image to sd card.This is the code:

public class MainActivity extends Activity {
    ImageView bmImage; 
    LinearLayout view1;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          view1 = (LinearLayout)findViewById(R.id.certlinear);
          bmImage = (ImageView)findViewById(R.id.certimage);

        Button button =(Button)findViewById(R.id.btn);
        button.setOnClickListener(hello);

    }
        Button.OnClickListener hello= new Button.OnClickListener()
        {  
            @Override  
            public void onClick(View v)
            {   
                  View view = view1.getRootView();
                  //u can use any view of your View instead of TextView

                 if(view!=null)
                 {
                 System.out.println("view is not null.....");
                 view.setDrawingCacheEnabled(true);
                 view.buildDrawingCache();
                 Bitmap bm = view.getDrawingCache();

                 try
                 {
                 if(bm!=null)
                 {
                 System.out.println("bm is not null.....");
                 OutputStream fos = null;
                 File folder = new File(Environment.getExternalStorageDirectory().toString() + "/sample.JPEG");
                 boolean success = false;
                 if (!folder.exists()) {
                     success = folder.mkdirs();
                 }
                 if (!success) {
                     // Do something on success
                 } else {
                     // Do something else on failure 
                 }
                 Toast.makeText(MainActivity.this,"path of the folder is "+folder.getAbsolutePath(), Toast.LENGTH_SHORT).show();

                 fos = new FileOutputStream(folder);

                 BufferedOutputStream bos = new BufferedOutputStream(fos);
                 bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);

                 bos.flush();
                 bos.close();
                 view.setDrawingCacheEnabled(false);
                 }
                 }
                 catch(Exception e)
                 {
                 System.out.println("Error="+e);
                 e.printStackTrace();
                 }
                 }

            }
        };
}

这是异常日志:

09-13 18:44:18.957: W/System.err(3612): java.io.FileNotFoundException: /sdcard/sample.JPEG
09-13 18:44:18.967: W/System.err(3612):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
09-13 18:44:18.967: W/System.err(3612):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
09-13 18:44:18.967: W/System.err(3612):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
09-13 18:44:18.976: W/System.err(3612):     at com.example.mytest.MainActivity$1.onClick(MainActivity.java:96)
09-13 18:44:18.976: W/System.err(3612):     at android.view.View.performClick(View.java:2364)
09-13 18:44:18.976: W/System.err(3612):     at android.view.View.onTouchEvent(View.java:4179)
09-13 18:44:18.976: W/System.err(3612):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
09-13 18:44:18.976: W/System.err(3612):     at android.view.View.dispatchTouchEvent(View.java:3709)
09-13 18:44:18.976: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.987: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.987: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.987: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.997: W/System.err(3612):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
09-13 18:44:18.997: W/System.err(3612):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-13 18:44:18.997: W/System.err(3612):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
09-13 18:44:18.997: W/System.err(3612):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-13 18:44:18.997: W/System.err(3612):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-13 18:44:18.997: W/System.err(3612):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 18:44:18.997: W/System.err(3612):     at android.os.Looper.loop(Looper.java:123)
09-13 18:44:18.997: W/System.err(3612):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-13 18:44:19.011: W/System.err(3612):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 18:44:19.011: W/System.err(3612):     at java.lang.reflect.Method.invoke(Method.java:521)
09-13 18:44:19.011: W/System.err(3612):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-13 18:44:19.011: W/System.err(3612):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-13 18:44:19.011: W/System.err(3612):     at dalvik.system.NativeStart.main(Native Method)

我在这行获得空指针异常:

I am getting null pointer exception in this line:

   fos = new FileOutputStream(folder);

先谢谢了。

推荐答案

只是简单的改变您的code需求,尝试如下,

Just simple change needs in your code, try as following,

 File folder = new File(Environment.getExternalStorageDirectory().toString());
             boolean success = false;
             if (!folder.exists()) 
             {
                 success = folder.mkdirs();
             }

     File file = new File(Environment.getExternalStorageDirectory().toString() + "/sample.JPEG");
         if ( !file.exists() )
         {
               success = file.createFile();
         }

这篇关于获得空指针异常而图像保存到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆