Android-Go上的MediaStore.ACTION_IMAGE_CAPTURE

时间:2019-02-05 11:27:10

标签: android mediastore image-capture

我拥有一个运行良好的应用程序,直到使用该应用程序的企业购买了每个运行 android go 的华为Y5lite,然后该应用程序在拍摄图像时开始崩溃。最近四天我一直在努力破解,但没有成功。崩溃是随机,并且不遵循任何模式,这意味着应用程序可以拍摄多张图像而不会崩溃,然后在某个时间崩溃,这让我觉得我的活动在背景和前景中的摄像头时被杀死了,因此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;
}
  

以下是当崩溃发生时我得到的错误。 (有时   拍照和处理效果很好,有时会发生崩溃)

     

02-05 13:53:22.139 23081-23086 / com.avigail.tuborg I /合子:做部分   代码缓存集合,代码= 25KB,数据= 30KB 02-05 13:53:22.140   23081-23086 / com.avigail.tuborg I / zygote:收集代码缓存后,   代码= 25KB,数据= 30KB 02-05 13:53:22.141 23081-23086 / com.avigail.tuborg   I /合子:将代码缓存容量增加到128KB 02-05 13:53:22.277   23081-23081 / com.avigail.tuborg D / AndroidRuntime:关闭VM   02-05 13:53:22.284 23081-23081 / com.avigail.tuborg E / AndroidRuntime:   致命异常:主要       流程:com.avigail.tuborg,PID:23081       java.lang.RuntimeException:无法恢复活动{com.avigail.tuborg / com.avigail.kaskazi.activities.StockistActivity}:   java.lang.RuntimeException:无法交付结果   ResultInfo {who = null,request = 1,result = -1,data = null}到活动   {com.avigail.tuborg / com.avigail.kaskazi.activities.StockistActivity}:   java.lang.NullPointerException:尝试调用虚拟方法'int   空对象引用上的java.lang.String.hashCode()'           在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3844)           在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1777)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:166)           在android.app.ActivityThread.main(ActivityThread.java:6861)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:450)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)        由以下原因引起:java.lang.RuntimeException:无法将结果ResultInfo {who = null,request = 1,result = -1,data = null}传递给活动   {com.avigail.tuborg / com.avigail.kaskazi.activities.StockistActivity}:   java.lang.NullPointerException:尝试调用虚拟方法'int   空对象引用上的java.lang.String.hashCode()'           在android.app.ActivityThread.deliverResults(ActivityThread.java:4564)           在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816)           在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1777)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:166)           在android.app.ActivityThread.main(ActivityThread.java:6861)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:450)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)        原因:java.lang.NullPointerException:尝试在null对象上调用虚拟方法'int java.lang.String.hashCode()'   参考           在com.avigail.kaskazi.activities.StockistActivity.processAndSetImage(StockistActivity.java:392)           在com.avigail.kaskazi.activities.StockistActivity.onActivityResult(StockistActivity.java:377)           在android.app.Activity.dispatchActivityResult(Activity.java:7393)           在android.app.ActivityThread.deliverResults(ActivityThread.java:4560)           在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3816)           在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3884)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3053)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1777)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:166)           在android.app.ActivityThread.main(ActivityThread.java:6861)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:450)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

非常感谢您的帮助 PS:我看到了解决该问题的各种问题,但是没有一个有效的答案,包括检查数据是否为空(我的数据为空,因为要添加额外的选项),将图像URL保存在SaveInstanceInstanceState上,znc检查是否意图结果返回确定

1 个答案:

答案 0 :(得分:0)

事实证明这是一个很愚蠢的错误,使我花费了几天的时间。 如我所料,问题是我的活动在后台被杀死,解决方案正确保存了状态。

好的,我保存了imageUrl,但是在用于检查已捕获哪个图像的开关的变量上丢失了

switch (imageTaken) {
        case "imageOne":

因此添加变量onSaveInstanceState并适当地对其进行调用就可以解决问题

@Override
    public void onSaveInstanceState(Bundle bundle)
    {
        super.onSaveInstanceState(bundle);
        bundle.putString("fileName", mTempPhotoPath);
        bundle.putString("imageTaken", imageTaken);
    }

如果您由于类似的问题而在这里,请检查是否没有变量会被重新初始化。

好处是,整个问题有助于优化我的图像大小以及位图处理期间的内存使用量