【问题标题】:android - app crashes when saving photographandroid - 保存照片时应用程序崩溃
【发布时间】:2014-03-20 13:03:35
【问题描述】:

我一直在关注http://developer.android.com/guide/topics/media/camera.html的相机指南

在我拍摄照片并选择勾选接受图像之前,一切正常。然后我得到可怕的“不幸的是应用程序已停止工作”。我正在真正的 nexus 5 设备上进行测试。

我的代码是

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

/* Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
} 


private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

/* Create a File for saving an image or video */
private static File getOutputMediaFile(int type){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "SimpleCamera");

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("SimpleCamera", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

public void openCamera(View view){
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

在我的清单中包含

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />

我的日志猫

03-20 12:40:53.658: E/AndroidRuntime(21487): FATAL EXCEPTION: main
03-20 12:40:53.658: E/AndroidRuntime(21487): Process: com.juliette.simplecamera, PID: 21487
03-20 12:40:53.658: E/AndroidRuntime(21487): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.juliette.simplecamera/com.juliette.simplecamera.MainActivity}: java.lang.NullPointerException
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.os.Looper.loop(Looper.java:136)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at java.lang.reflect.Method.invoke(Method.java:515)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at dalvik.system.NativeStart.main(Native Method)
03-20 12:40:53.658: E/AndroidRuntime(21487): Caused by: java.lang.NullPointerException
03-20 12:40:53.658: E/AndroidRuntime(21487):    at com.juliette.simplecamera.MainActivity.onActivityResult(MainActivity.java:94)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
03-20 12:40:53.658: E/AndroidRuntime(21487):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)

还有我的布局以防万一

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.juliette.simplecamera.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="58dp"
    android:text="@string/camera" 
    android:onClick="openCamera"/>

</RelativeLayout>

我知道问题与返回原始活动有关,但我不知道如何解决这个问题。任何建议都将不胜感激,我对此很陌生,我假设我犯了一个简单的新手错误。

提前致谢!

【问题讨论】:

  • MainActivity.java 类的第 94 行是什么?
  • 我认为Intent datanull in onActivityResult()...
  • 是 data.getData(), Toast.LENGTH_LONG).show();从吐司
  • 数据为空。请检查以下答案。
  • 谢谢大家,简单的修复可能会让我多花几个小时的沮丧。非常感谢!

标签: java android android-camera


【解决方案1】:

在这行发生崩溃...因为,datanull

Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();

这行说datanull...

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}

因此,当您尝试使用getData() 方法从Intent 对象data 获取数据时,会发生NullPointerException

为防止出现这种Exception,请在尝试获取以下数据之前检查data 不是null...

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {

        if(data != null) {

            Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
        }
    }
}

【讨论】:

  • 谢谢,这很有意义
【解决方案2】:

从下面的 logcat 中,它清楚地表明您的 Intent datanull

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}

检查Intent 数据的空值。

尝试如下:

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK && null != data) {

        // Image captured and saved to fileUri specified in the Intent
        Toast.makeText(this, "Image saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show();
    } 

【讨论】:

  • 有时我们会收到null 数据,即使结果是成功的,即结果代码是Activity.RESULT_OK。所以用Intent data 验证不是这里的方法。
  • 行得通!非常感谢,我知道这很简单
【解决方案3】:

试试这个

Toast.makeText(MainActivity.this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();

【讨论】:

    猜你喜欢
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    相关资源
    最近更新 更多