MediaStore.ACTION_IMAGE_CAPTURE 在 Android-开始
MediaStore.ACTION_IMAGE_CAPTURE on Android-Go
我有这个应用程序,在使用该应用程序的企业购买每个人华为 Y5lite 运行 android go 之前一直运行良好,然后该应用程序开始崩溃拍照时。在过去的四天里,我一直在努力破解这个问题,但没有成功。崩溃是 随机的 并且不遵循任何模式,这意味着该应用程序可以拍摄多张图像而不会崩溃然后在某个时候崩溃这让我觉得我的 activity 在前景上的背景和相机等 onActivityResult 失败。我已经尝试保存图像 URL onSaveInstanceState 并在 onCreate 上检索它但没有解决下面是我的相关代码和错误
private void launchCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = BitmapUtils.createTempImageFile(this);
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
mTempPhotoPath = photoFile.getAbsolutePath();
// Get the content URI for the image file
Uri photoURI = FileProvider.getUriForFile(this,
FILE_PROVIDER_AUTHORITY,
photoFile);
// Add the URI so the camera can store the image
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// Launch the camera activity
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
//saving the filename incase activity is killed
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", mTempPhotoPath);
}
//retrieving file name onCreate
if (savedInstanceState != null){
mTempPhotoPath = savedInstanceState.getString("fileName");}
// processing the image
private void processAndSetImage() {
switch (imageTaken) {
case "imageOne":
//I did the try-catch to see if I can isolate the issue but still crashes
try{
mAddImageOne.setVisibility(View.GONE);
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
mOneView.setVisibility(View.VISIBLE);
mOneView.setImageBitmap(mResultsBitmap);
findViewById(R.id.front_image_text).setVisibility(View.VISIBLE);
findViewById(R.id.save_cancel).setVisibility(View.VISIBLE);
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Please try take the picture again",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 1100);
toast.show();
}
break;
case "imageTwo":
try{
mAddImageTwo.setVisibility(View.GONE);
mTwoView.setImageBitmap(mResultsBitmap);
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
findViewById(R.id.front_image_label_text).setVisibility(View.VISIBLE);
findViewById(R.id.save_cancel).setVisibility(View.VISIBLE);
mTwoView.setVisibility(View.VISIBLE);
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Please try take the picture again",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 1100);
toast.show();
}
break;
}
Below is the error I am getting when the crash occurs. (Sometimes the
photo taking and processing occurs well, sometime the crash happens)
02-05 13:53:22.139 23081-23086/com.avigail.tuborg I/zygote: Do partial
code cache collection, code=25KB, data=30KB 02-05 13:53:22.140
23081-23086/com.avigail.tuborg I/zygote: After code cache collection,
code=25KB, data=30KB 02-05 13:53:22.141 23081-23086/com.avigail.tuborg
I/zygote: Increasing code cache capacity to 128KB 02-05 13:53:22.277
23081-23081/com.avigail.tuborg D/AndroidRuntime: Shutting down VM
02-05 13:53:22.284 23081-23081/com.avigail.tuborg E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.avigail.tuborg, PID: 23081
java.lang.RuntimeException: Unable to resume activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}:
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=1, result=-1, data=null} to activity
{com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.String.hashCode()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3844)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity
{com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'int
java.lang.String.hashCode()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4564)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object
reference
at com.avigail.kaskazi.activities.StockistActivity.processAndSetImage(StockistActivity.java:392)
at com.avigail.kaskazi.activities.StockistActivity.onActivityResult(StockistActivity.java:377)
at android.app.Activity.dispatchActivityResult(Activity.java:7393)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4560)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
非常感谢你的帮助
PS:我看到了解决该问题的各种问题,但 none 的答案有效,包括检查数据是否为空(我的数据为空,因为我放置了额外的选项),保存图像 URL onSaveInstanceState,znc 检查 intentfor 结果是否 returns ok
事实证明这是一个相当愚蠢的错误,让我花了好几天时间。
正如我预料的那样,问题是我的 activity 在后台被杀死,解决方案是正确保存状态。
我正在保存 imageUrl,好的,但是在检查捕获了哪个图像的开关中缺少变量
switch (imageTaken) {
case "imageOne":
因此添加变量 onSaveInstanceState 并在 onCreate 中适当调用它解决了问题
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", mTempPhotoPath);
bundle.putString("imageTaken", imageTaken);
}
如果您因为类似的问题发现自己在这里,请检查是否没有变量不会用值初始化,以防您的 activity 被重新创建。
好处是整个问题有助于优化我的图像大小,以及位图处理期间的内存使用
我有这个应用程序,在使用该应用程序的企业购买每个人华为 Y5lite 运行 android go 之前一直运行良好,然后该应用程序开始崩溃拍照时。在过去的四天里,我一直在努力破解这个问题,但没有成功。崩溃是 随机的 并且不遵循任何模式,这意味着该应用程序可以拍摄多张图像而不会崩溃然后在某个时候崩溃这让我觉得我的 activity 在前景上的背景和相机等 onActivityResult 失败。我已经尝试保存图像 URL onSaveInstanceState 并在 onCreate 上检索它但没有解决下面是我的相关代码和错误
private void launchCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = BitmapUtils.createTempImageFile(this);
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
mTempPhotoPath = photoFile.getAbsolutePath();
// Get the content URI for the image file
Uri photoURI = FileProvider.getUriForFile(this,
FILE_PROVIDER_AUTHORITY,
photoFile);
// Add the URI so the camera can store the image
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// Launch the camera activity
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
//saving the filename incase activity is killed
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", mTempPhotoPath);
}
//retrieving file name onCreate
if (savedInstanceState != null){
mTempPhotoPath = savedInstanceState.getString("fileName");}
// processing the image
private void processAndSetImage() {
switch (imageTaken) {
case "imageOne":
//I did the try-catch to see if I can isolate the issue but still crashes
try{
mAddImageOne.setVisibility(View.GONE);
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
mOneView.setVisibility(View.VISIBLE);
mOneView.setImageBitmap(mResultsBitmap);
findViewById(R.id.front_image_text).setVisibility(View.VISIBLE);
findViewById(R.id.save_cancel).setVisibility(View.VISIBLE);
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Please try take the picture again",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 1100);
toast.show();
}
break;
case "imageTwo":
try{
mAddImageTwo.setVisibility(View.GONE);
mTwoView.setImageBitmap(mResultsBitmap);
mResultsBitmap = BitmapUtils.resamplePic(this, mTempPhotoPath);
findViewById(R.id.front_image_label_text).setVisibility(View.VISIBLE);
findViewById(R.id.save_cancel).setVisibility(View.VISIBLE);
mTwoView.setVisibility(View.VISIBLE);
}catch (Exception e){
Toast toast = Toast.makeText(getApplicationContext(), "Please try take the picture again",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 1100);
toast.show();
}
break;
}
Below is the error I am getting when the crash occurs. (Sometimes the photo taking and processing occurs well, sometime the crash happens)
02-05 13:53:22.139 23081-23086/com.avigail.tuborg I/zygote: Do partial code cache collection, code=25KB, data=30KB 02-05 13:53:22.140 23081-23086/com.avigail.tuborg I/zygote: After code cache collection, code=25KB, data=30KB 02-05 13:53:22.141 23081-23086/com.avigail.tuborg I/zygote: Increasing code cache capacity to 128KB 02-05 13:53:22.277 23081-23081/com.avigail.tuborg D/AndroidRuntime: Shutting down VM 02-05 13:53:22.284 23081-23081/com.avigail.tuborg E/AndroidRuntime: FATAL EXCEPTION: main Process: com.avigail.tuborg, PID: 23081 java.lang.RuntimeException: Unable to resume activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3844) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.avigail.tuborg/com.avigail.kaskazi.activities.StockistActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4564) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference at
com.avigail.kaskazi.activities.StockistActivity.processAndSetImage(StockistActivity.java:392)
atcom.avigail.kaskazi.activities.StockistActivity.onActivityResult(StockistActivity.java:377)
at android.app.Activity.dispatchActivityResult(Activity.java:7393) at android.app.ActivityThread.deliverResults(ActivityThread.java:4560) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:6861) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450) atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
非常感谢你的帮助 PS:我看到了解决该问题的各种问题,但 none 的答案有效,包括检查数据是否为空(我的数据为空,因为我放置了额外的选项),保存图像 URL onSaveInstanceState,znc 检查 intentfor 结果是否 returns ok
事实证明这是一个相当愚蠢的错误,让我花了好几天时间。 正如我预料的那样,问题是我的 activity 在后台被杀死,解决方案是正确保存状态。
我正在保存 imageUrl,好的,但是在检查捕获了哪个图像的开关中缺少变量
switch (imageTaken) {
case "imageOne":
因此添加变量 onSaveInstanceState 并在 onCreate 中适当调用它解决了问题
@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
bundle.putString("fileName", mTempPhotoPath);
bundle.putString("imageTaken", imageTaken);
}
如果您因为类似的问题发现自己在这里,请检查是否没有变量不会用值初始化,以防您的 activity 被重新创建。
好处是整个问题有助于优化我的图像大小,以及位图处理期间的内存使用