我正在制作照片编辑应用程序。当我从图库中选择任何图像时,图像成功移动到编辑活动。但是如果我使用相机点击图片,点击图片后应用程序崩溃。单击的图片不会发送到编辑活动。我在代码中看不到任何错误,并且 logcat 显示了我不理解的内容。已经找不到类似的问题/解决方案。任何帮助将非常感激。
int IMAGE_REQUEST_CODE = 45;
int CAMERA_REQUEST_CODE = 14;
int RESULT_CODE = 200;
binding.cameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ActivityCompat.checkSelfPermission(MainActivity.this ,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this ,
new String[] {Manifest.permission.CAMERA} , 32);
} else {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent , CAMERA_REQUEST_CODE);
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST_CODE){
if (data.getData() != null){
Uri filepath = data.getData();
Intent dsPhotoEditorIntent = new Intent(this, DsPhotoEditorActivity.class);
dsPhotoEditorIntent.setData(filepath);
dsPhotoEditorIntent.putExtra(DsPhotoEditorConstants.DS_PHOTO_EDITOR_OUTPUT_DIRECTORY, "Photon");
//dsPhotoEditorIntent.putExtra(DsPhotoEditorConstants.DS_TOOL_BAR_BACKGROUND_COLOR, Color.parseColor("#000000"));
//dsPhotoEditorIntent.putExtra(DsPhotoEditorConstants.DS_MAIN_BACKGROUND_COLOR, Color.parseColor("#8A8888"));
int[] toolsToHide = {DsPhotoEditorActivity.TOOL_ORIENTATION, DsPhotoEditorActivity.TOOL_CROP};
dsPhotoEditorIntent.putExtra(DsPhotoEditorConstants.DS_PHOTO_EDITOR_TOOLS_TO_HIDE, toolsToHide);
startActivityForResult(dsPhotoEditorIntent, RESULT_CODE);
}
}
if (requestCode == RESULT_CODE){
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.setData(data.getData());
startActivity(intent);
}
if (requestCode == CAMERA_REQUEST_CODE){
Bitmap photo = (Bitmap) data.getExtras().get("data");
Uri uri = getImageUri(photo);
Intent dsPhotoEditorIntent = new Intent(MainActivity.this, DsPhotoEditorActivity.class);
//dsPhotoEditorIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
dsPhotoEditorIntent.setData(uri);
dsPhotoEditorIntent.putExtra(DsPhotoEditorConstants.DS_PHOTO_EDITOR_OUTPUT_DIRECTORY, "Photon");
int[] toolsToHide = {DsPhotoEditorActivity.TOOL_ORIENTATION, DsPhotoEditorActivity.TOOL_CROP};
dsPhotoEditorIntent.putExtra(DsPhotoEditorConstants.DS_PHOTO_EDITOR_TOOLS_TO_HIDE, toolsToHide);
startActivityForResult(dsPhotoEditorIntent, RESULT_CODE);
}
}
public Uri getImageUri(Bitmap bitmap) {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, arrayOutputStream);
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Title", null);
return Uri.parse(path);
}
日志猫:
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=14, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.photon/com.example.photon.MainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:5384)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4835)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2319)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:257)
at android.app.ActivityThread.main(ActivityThread.java:8212)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)
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.example.photon.MainActivity.getImageUri(MainActivity.java:140)
at com.example.photon.MainActivity.onActivityResult(MainActivity.java:123)
at android.app.Activity.dispatchActivityResult(Activity.java:8429)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5377)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4790)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4835)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2319)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:257)
at android.app.ActivityThread.main(ActivityThread.java:8212)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)