java Android应用程序崩溃后,几个图像拍摄

6ss1mwsb  于 2023-02-11  发布在  Java
关注(0)|答案(1)|浏览(84)

我正在为Android开发一个应用程序,它应该从相机拍照并将这些图像上传到数据库。然而,该应用程序在前8-10张照片中运行良好,但当我试图拍摄更多照片时,该应用程序崩溃。
日志中的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.salesservice, PID: 18379
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.project.salesservice/com.project.salesservice.user.UserVisitDetailActivity}: java.lang.NullPointerException: uriString
    at android.app.ActivityThread.deliverResults(ActivityThread.java:5195)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5236)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8057)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
 Caused by: java.lang.NullPointerException: uriString
    at android.net.Uri$StringUri.<init>(Uri.java:496)
    at android.net.Uri$StringUri.<init>(Uri.java:486)
    at android.net.Uri.parse(Uri.java:458)
    at com.project.salesservice.user.UserVisitDetailActivity.getImageUri(UserVisitDetailActivity.java:582)
    at com.project.salesservice.user.UserVisitDetailActivity.onActivityResult(UserVisitDetailActivity.java:558)
    at android.app.Activity.dispatchActivityResult(Activity.java:8516)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:5188)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5236) 
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:236) 
    at android.app.ActivityThread.main(ActivityThread.java:8057) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011) 

启动意图代码

imageUri = null;
    Intent takePictureIntent = new Intent();
    takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
    takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

活动结果代码

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

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == UserVisitDetailActivity.RESULT_OK && data != null){
        System.out.println("Masuk Camera");

        Bitmap photo = (Bitmap) data.getExtras().get("data");

        imageUri = getImageUri(getApplicationContext(), photo);

        if(checkInOut.getText().equals("Check In")) {
            System.out.println(imageUri);
            checkInHolder.setVisibility(View.VISIBLE);
            checkInHolder.setImageURI(imageUri);
        } else if(checkInHolder.getVisibility() == View.VISIBLE && checkInOut.getText().equals("Check Out")) {
            System.out.println(imageUri);
            checkOutHolder.setVisibility(View.VISIBLE);
            checkOutHolder.setImageURI(imageUri);
        }
    }
}

获取图像URI代码

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

删除存储在图库中的照片后问题解决。如果图库中的照片达到10张,应用程序将再次崩溃。如果我的语法不正确,请原谅。非常感谢您的帮助。谢谢

nqwrtyyt

nqwrtyyt1#

也许你可以优化你的图像拍摄。设备上的内存不足可能是这个问题的原因之一。如果没有足够的内存,程序可能会崩溃,因为拍摄照片耗尽内存。你可以考虑压缩照片之前提交到数据库或放置一个系统,释放内存后,每张照片被采取,以解决这个问题。
您还可以检查对 MediaStore.Images.Media.insertImage 方法的调用是否返回空字符串,然后将该空字符串传递给 Uri.parse 方法,从而导致NullPointerException。您应该检查 MediaStore.Images.Media.insertImage 方法是否实际返回有效值。如果没有,您应该考虑使用其他方法将图像插入内部存储。

相关问题