捕获照片时模拟器崩溃(NullPointerException)

时间:2013-01-06 21:47:48

标签: android android-camera

拍摄照片时我遇到了一些问题。

到现在为止,我可以从相机,模拟器和真实设备拍摄照片,没有任何问题。但这些图片是保存在默认路线上的。

我想编辑那条路线,所以我可以设置自己的路线。

我是这样做的:

File folder = new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/");
            folder.mkdirs();

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            Uri uriSavedImage=Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/imatge.jpg"));
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            startActivityForResult(cameraIntent, 1888);

这是在真正的设备,索尼爱立信Xperia Ray。它创建文件夹,并将图像保存在那里。

但问题出在仿真器上。我担心这会发生在其他设备上。

这是logcat:

01-06 21:35:21.115: E/AndroidRuntime(1565): FATAL EXCEPTION: main
01-06 21:35:21.115: E/AndroidRuntime(1565): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=264032, result=-1, data=null} to activity {com.exercise.AndroidViewPager/com.exercise.AndroidViewPager.AndroidViewPagerActivity}: java.lang.NullPointerException
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.os.Looper.loop(Looper.java:137)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at java.lang.reflect.Method.invoke(Method.java:511)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at dalvik.system.NativeStart.main(Native Method)
01-06 21:35:21.115: E/AndroidRuntime(1565): Caused by: java.lang.NullPointerException
01-06 21:35:21.115: E/AndroidRuntime(1565):     at com.exercise.AndroidViewPager.MyFragmentD.onActivityResult(MyFragmentD.java:52)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:152)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
01-06 21:35:21.115: E/AndroidRuntime(1565):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
01-06 21:35:21.115: E/AndroidRuntime(1565):     ... 11 more

崩溃:

01-06 21:35:21.115: E/AndroidRuntime(1565): Caused by: java.lang.NullPointerException
01-06 21:35:21.115: E/AndroidRuntime(1565):     at com.exercise.AndroidViewPager.MyFragmentD.onActivityResult(MyFragmentD.java:52)

来自这里:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if( requestCode == 1888 && resultCode == -1) { //-1 = TOT HA ANAT BE.
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        Log.d("debugging",""+photo.getHeight());
        Log.d("debugging",""+photo.getWidth());
        ((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(photo);
    }
}

完全:

Bitmap photo = (Bitmap) data.getExtras().get("data");
经过一番研究,我发现没什么好看的......

你能帮我解决这个问题吗?

感谢。

4 个答案:

答案 0 :(得分:2)

由于您的数据附加方法不起作用,您可以尝试在启动Intent时存储文件的字段或路径。

File resultingFile;//This will be in your class definition

File folder = new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/");
folder.mkdirs();

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile = new File(folder.toString() + "/image.jpg");
Uri uriSavedImage=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

startActivityForResult(cameraIntent, 1888);

因此,当您输入onActivityResult时,您可以针对RESULT_OK和RESULT_CANCELED测试resultCode。当我使用onActivityResult时,我这样做:

if(requestCode == 1888){
  if(resultCode == RESULT_OK){ //Called when the user saves the image
     if(resultingFile.exists(){
       Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath()); 
       ((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(photo);
     }
  }else if(resultCode == RESULT_CANCELED){ //Called when the user presses back or cancels the saving of the image

  }
}

答案 1 :(得分:1)

经过一些帮助,特别是来自@BackpackOnHead,我明白了。

我取得了什么成就?

  • 从相机中检索图片
  • 将其保存在指定的文件夹
  • 将其保存在指定名称

这是最终代码:

关于上课的声明:

File resultingFile;

单击启动摄像头的按钮时:

public void onClick(View v) {
            Log.d("debugging","Has clicat al botó");

            File folder = new File(Environment.getExternalStorageDirectory().toString()+"/FolderWithImages/"); //NAME OF THE FOLDER
            folder.mkdirs();

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            resultingFile=new File(folder.toString() + "/imatge.jpg"); //THIS IS THE NAME OF THE FILE
            Uri uriSavedImage=Uri.fromFile(resultingFile);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

            startActivityForResult(cameraIntent, 1888);
        }   

然后,onActivityResult:

Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
        Log.d("debugging",resultingFile.getPath());
        ((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(photo);

耶!

为什么我这样做而不是Bitmap photo = (Bitmap) data.getExtras().get("data");

基本上,因为对于某些未知(对我来说)的原因,在某些设备上,如同在模拟器上一样,这个.getExtras返回null,在某些设备中,显而易见的是,它应该不返回null。这张“getExtras”提供了照片。我们不得不使用一种解决方法,这可能不是清晰干净,但它可以解决问题。

答案 2 :(得分:0)

getExtras()可以返回null,在你的情况下它可能会返回null。

答案 3 :(得分:0)

试试这个:

File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, f.getAbsolutePath());
startActivityForResult(takePictureIntent, 1);