android关于调用系统相机拍照后APP闪退的问题

最近在写一个小APP,就是调用系统相机拍照,然后保存在本地相册,代码没有显示bug,但是在调试时出现了拍照后闪退的问题,我的测试机是小米2S,MIUI 7系统,android5.0.X的,拍照的照片能保存在本地,但是在拍照后点击确定后软件就闪退了,这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);

    mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 调用系统相机  
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
            intent.addCategory(Intent.CATEGORY_DEFAULT);  
            // 取当前时间为照片名  
            String name = DateFormat.format("yyyyMMdd_hhmmss",  
                    Calendar.getInstance(Locale.CHINA))  
                    + ".jpg";  
            Log.d("onactivity", "mPictureFile:" + name);  
            picPath = getPhotopath() + name;  
            // 通过文件创建一个uri中  
            Uri imageUri = Uri.fromFile(new File(picPath));  
            // 保存uri对应的照片于指定路径  
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
            startActivityForResult(intent, OPEN_RESULT2); 
        }
    });
}

/**
 * 获取原图片存储路径
 * 
 * @return
 */
@SuppressLint("SimpleDateFormat") 
private String getPhotopath() {
    // 照片全路径
    String fileName = "";
    // 文件夹路径
    String pathUrl = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";

    File file = new File(pathUrl);
    file.mkdirs();// 创建文件夹
    fileName = pathUrl + fileName;
    return fileName;
}

5个回答

这是日志,只进行了点击拍照操作,拍完后就闪退了
09-14 19:43:19.296: W/Toast(32157): From com.example.newcamera, go ahead.
09-14 19:43:19.306: D/AndroidRuntime(32157): Shutting down VM
09-14 19:43:19.306: E/AndroidRuntime(32157): FATAL EXCEPTION: main
09-14 19:43:19.306: E/AndroidRuntime(32157): Process: com.example.newcamera, PID: 32157
09-14 19:43:19.306: E/AndroidRuntime(32157): java.lang.RuntimeException: Unable to resume activity {com.example.newcamera/com.example.newcamera.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.newcamera/com.example.newcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2954)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2985)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.access$800(ActivityThread.java:147)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.os.Looper.loop(Looper.java:135)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.main(ActivityThread.java:5235)
09-14 19:43:19.306: E/AndroidRuntime(32157): at java.lang.reflect.Method.invoke(Native Method)
09-14 19:43:19.306: E/AndroidRuntime(32157): at java.lang.reflect.Method.invoke(Method.java:372)
09-14 19:43:19.306: E/AndroidRuntime(32157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
09-14 19:43:19.306: E/AndroidRuntime(32157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)
09-14 19:43:19.306: E/AndroidRuntime(32157): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.newcamera/com.example.newcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.deliverResults(ActivityThread.java:3542)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
09-14 19:43:19.306: E/AndroidRuntime(32157): ... 11 more
09-14 19:43:19.306: E/AndroidRuntime(32157): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
09-14 19:43:19.306: E/AndroidRuntime(32157): at com.example.newcamera.MainActivity.onActivityResult(MainActivity.java:110)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.Activity.dispatchActivityResult(Activity.java:6143)
09-14 19:43:19.306: E/AndroidRuntime(32157): at android.app.ActivityThread.deliverResults(ActivityThread.java:3538)
09-14 19:43:19.306: E/AndroidRuntime(32157): ... 12 more

91program
91program Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference,这一句是关键,查查吧
接近 5 年之前 回复

我去,代码怎么开头几行没进来,,,,,,

调试一下,抓一下出错时的 LOG 来分析吧
PS: 小米的手机,都是外包的软件,所以软件系统比较混乱。不建议用来做调试,如果你是做通用的软件,最后需要验证能否在小米上正常运行。否则,就不要分析小米的手机上不能运行的原因了。

qq_20615629
七哥有个小梦想 放出来了,在下边呢,这几天开学没怎么搞
接近 5 年之前 回复

建议下吧错误日志贴出来。。

qq_20615629
七哥有个小梦想 贴出来了,下边呢
接近 5 年之前 回复

有时候换个系统默认相机 调用差别很大

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐