将位图保存到SD卡时出现NullPointerException

时间:2014-03-21 10:57:25

标签: android eclipse nullpointerexception bundle android-bitmap

我试图拍照并将其保存到SD中,但在某个时刻(照片拍完后)我得到了NullPointerException。这是代码:

    package com.social.bearv2;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Articolo extends Activity {
 private static final int CAPTURE_IMAGE_CAPTURE_CODE = 0;
 Intent i;
 private Button ib;
 private ImageView imview;

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.article);
  ib = (Button) findViewById(R.id.insert);
  imview = (ImageView) findViewById(R.id.imview);

   ib.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       File photoFile = null;
       try {
           photoFile = createImageFile();
       } catch (IOException ex) {
           // Error occurred while creating the File
       }
       // Continue only if the File was successfully created
       if (photoFile != null) {
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    i.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photoFile));
    startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
   }
   }
  });
 }
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
   if (resultCode == RESULT_OK) {
       try {
    Toast.makeText(this, "Image Captured", Toast.LENGTH_LONG).show();
    Bundle extras = data.getExtras();
    Bitmap imageBitmap = (Bitmap) extras.get("data");  
    imview.setImageBitmap(imageBitmap);
   } catch (NullPointerException ex) {

   }
   } else if (resultCode == RESULT_CANCELED) {
    Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
   }
  }
 }

 private File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "BeAR_" + timeStamp + ".jpg";
        File photo = new File(Environment.getExternalStorageDirectory(),  imageFileName);
        return photo;
    }

}

请注意,给我异常的代码行是" Bundle extras = data.getExtras();"

这是logcat:

03-21 11:41:32.140: E/AndroidRuntime(24225): FATAL EXCEPTION: main
03-21 11:41:32.140: E/AndroidRuntime(24225): Process: com.social.bearv2, PID: 24225
03-21 11:41:32.140: E/AndroidRuntime(24225): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.social.bearv2/com.social.bearv2.Articolo}: java.lang.NullPointerException
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3385)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3428)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.ActivityThread.access$1300(ActivityThread.java:145)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.os.Looper.loop(Looper.java:136)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.ActivityThread.main(ActivityThread.java:5081)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at java.lang.reflect.Method.invoke(Method.java:515)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at dalvik.system.NativeStart.main(Native Method)
03-21 11:41:32.140: E/AndroidRuntime(24225): Caused by: java.lang.NullPointerException
03-21 11:41:32.140: E/AndroidRuntime(24225):    at com.social.bearv2.Articolo.onActivityResult(Articolo.java:61)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
03-21 11:41:32.140: E/AndroidRuntime(24225):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3381)

有什么建议吗?

4 个答案:

答案 0 :(得分:1)

此处Intent datanull ... stacktrace表示datanull ...

ResultInfo{who=null, request=0, result=-1, data=null // here}

您正在null

访问Bundle extras = data.getExtras();个实例

在传递输出位置时捕获图像时不会收到任何数据。您捕获的图像将保存在photoFile位置(您创建的位置)...

答案 1 :(得分:0)

查看您的日志:

03-21 11:41:32.140: E/AndroidRuntime(24225): java.lang.RuntimeException: 
Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} 

它说数据为空。在您的onActivityResult中,您尝试访问数据:

 Bundle extras = data.getExtras();

答案 2 :(得分:0)

if(data != null)
{
     Bundle extras = data.getExtras();
 System.out.println("extras" + extras);
 if (extras != null)
 {
    bitmap = extras.getParcelable("data");
    //setting image resource
        imageView.setImageBitmap(bitmap);
     }
}
else
{
      Log.v("tag" , "data was null");
}

答案 3 :(得分:0)

ACTION_IMAGE_CAPTURE会将一个小的Bitmap图像传递给onActivityResult - 但前提是你没有在EXTRA_OUTPUT中提供文件URI。

您已指定应存储图像文件的位置,因此Android假定您将从该文件中读取图像。这就是onActivityResult中Intent为null的原因。