拍照后应用程序崩溃

时间:2016-10-15 08:22:26

标签: android

我正在使用此代码创建一个简单的相机应用程序。我可以拍张照片。但是,拍摄照片后应用程序崩溃了。

{
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);                       

    File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "abc.jpg");


    Uri store= Uri.fromFile(imageFile);
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, store);
    intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 0);
    startActivityForResult(intent, 0);
}

/ ********* onActivityResult ****** ********************************* /

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    if (imageFile.exists())
                    {
                        Toast.makeText(this, "The file was saved at " + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(this, "Error saving the file ", Toast.LENGTH_LONG).show();
                    }
                        break;
                case Activity.RESULT_CANCELED:
                            break;
                        default:
                            break;
                    }
            }
        }

下面是logcat。

  

java.lang.RuntimeException:将结果ResultInfo {who = null,request = 0,result = -1,data = null}传递给activity {com.camera/com.camera.MainActivity}失败:java.lang.NullPointerException :尝试在空对象引用上调用虚方法'boolean java.io.File.exists()'

     

在android.app.ActivityThread.deliverResults(ActivityThread.java:3758)

     

在android.app.ActivityThread.handleSendResult(ActivityThread.java:3801)

     

在android.app.ActivityThread.access $ 1400(ActivityThread.java:157)

     

在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1405)                        在android.os.Handler.dispatchMessage(Handler.java:102)                        在android.os.Looper.loop(Looper.java:148)

     

在android.app.ActivityThread.main(ActivityThread.java:5551)                        at java.lang.reflect.Method.invoke(Native Method)

     

at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:730)

     

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

     

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean java.io.File.exists()'

     

at com.camera.MainActivity.onActivityResult(MainActivity.java:43)

     

在android.app.Activity.dispatchActivityResult(Activity.java:6463)

     

在android.app.ActivityThread.deliverResults(ActivityThread.java:3754)

     

在android.app.ActivityThread.handleSendResult(ActivityThread.java:3801)

     

在android.app.ActivityThread.access $ 1400(ActivityThread.java:157)

     

在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1405)

     

在android.os.Handler.dispatchMessage(Handler.java:102)                           在android.os.Looper.loop(Looper.java:148)

     

在android.app.ActivityThread.main(ActivityThread.java:5551)                        at java.lang.reflect.Method.invoke(Native Method)

     

at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:730)

     

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

我无法弄清楚为什么会这样。有谁知道我该如何修复错误?谢谢!

4 个答案:

答案 0 :(得分:1)

当用户从“相机”单击操作导航回“活动”时,将重新创建活动。

因此,您需要将文件路径存储在[ { "label": [ "fattacq_an_eser_facq", "eu_tot_doc", "fattacq_prot_facq", "id_sogg", "eu_tot_man" ], "values": [ { "label": "Prima Fattura 2016", "values": [ 2016, 956.48, 691, 44633, 956.48 ] }, { "label": "Seconda Fattura 2016", "values": [ 2016, 190, 982, 38127, 190 ] }, { "label": "Terza Fattura 2016", "values": [ 2016, 140.3, 1088, 59381, 140.3 ] }, { "label": "Quarta Fattura 2016", "values": [ 2016, 488, 1091, 59382, 488 ] }, { "label": "Quinta Fattura 2016", "values": [ 2016, 11365.95, 1154, 57526, 11365.95 ] }, { "label": "Sesta Fattura 2016", "values": [ 2016, 44440.01, 1276, 5555, 44440.01 ] } ] } ] 内的Bundle中(确保在调用onSaveInstanceState() {{1}后调用super.onSaveInstanceState(outState); }} putString),然后在outState内再次创建文件。

答案 1 :(得分:0)

请检查您是否在应用==中配置了相应的权限
AndroidManifest.xml和/或READ_EXTERNAL_STORAGE

答案 2 :(得分:0)

您可以尝试从WRITE_EXTERNAL_STORAGE参数中检索图像URI:

Intent

答案 3 :(得分:0)

我唯一能想到的是你有两个imageFile变量,一个你定义并在意图中发送的变量,另一个imageFile全局变量总是为空。

此代码完美无缺:

public class CameraIntentActivity extends AppCompatActivity {

    private File imageFile;

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

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "abc.jpg");

        Uri store= Uri.fromFile(imageFile);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, store);
        intent.putExtra(android.provider.MediaStore.EXTRA_VIDEO_QUALITY, 0);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0) {
            switch (resultCode) {
                case Activity.RESULT_OK:
                    if (imageFile.exists())
                    {
                        Toast.makeText(this, "The file was saved at " + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(this, "Error saving the file ", Toast.LENGTH_LONG).show();
                    }
                    break;
                case Activity.RESULT_CANCELED:
                    break;
                default:
                    break;
            }
        }
    }