在Android Oreo中调用Camera Intent后,正在重新创建父级活动 [英] Parent Activity is recreating after calling Camera intent in Android Oreo

查看:55
本文介绍了在Android Oreo中调用Camera Intent后,正在重新创建父级活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用媒体意图捕获图像.处理完成后,结果将发送回给父级.以上提到的过程在Nougat Os之前都可以正常工作,但是在Oreo中,父活动将重新创建.我该如何解决这个问题.

I am using Media intent capturing image. Once process completed result will be send back to parent. Above mentioned process working properly up to Nougat Os but in Oreo the parent activity is recreating again. How can i solve this issue.

推荐答案

上面提到直到牛轧糖Os都可以正常工作的过程,但是在奥利奥,父活动正在重新创建

Above mentioned process working properly up to Nougat Os but in Oreo the parent activity is recreating again

当相机应用程序处于前台时,您的过程将终止.这是完全正常的,与Android 8.0无关.它与可用的系统RAM以及当时设备中正在发生的一切有关.

Your process is being terminated while the camera app is in the foreground. This is perfectly normal and has nothing to do with Android 8.0. It has everything to do with the available system RAM and what is all going on in the device at the time.

我该如何解决这个问题.

How can i solve this issue.

没有问题.当您没有前台UI时,您的进程可以随时终止.您的代码需要处理这个问题.

There is no issue. Your process can be terminated at any point when you do not have the foreground UI. Your code needs to deal with that.

例如,如果您在 ACTION_IMAGE_CAPTURE Intent 上使用 EXTRA_OUTPUT ,则需要记住该值,因为没有得到它以相机应用程序的任何形式返回结果.正如我在此示例应用,尤其是在此活动中:

For example, if you are using EXTRA_OUTPUT on your ACTION_IMAGE_CAPTURE Intent, you need to remember that value, as you do not get it back in any form of result from the camera app. Saving it in the saved instance state Bundle is a typical solution, as I illustrate in this sample app, particularly in this activity:

/***
 Copyright (c) 2008-2017 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 Covered in detail in the book _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.commonsware.android.camcon;

import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import java.io.File;
import java.util.List;

public class MainActivity extends Activity {
  private static final String EXTRA_FILENAME=
    "com.commonsware.android.camcon.EXTRA_FILENAME";
  private static final String FILENAME="CameraContentDemo.jpeg";
  private static final int CONTENT_REQUEST=1337;
  private static final String AUTHORITY=
    BuildConfig.APPLICATION_ID+".provider";
  private static final String PHOTOS="photos";
  private File output=null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState==null) {
      output=new File(new File(getFilesDir(), PHOTOS), FILENAME);

      if (output.exists()) {
        output.delete();
      }
      else {
        output.getParentFile().mkdirs();
      }

      Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

      i.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

      if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      }
      else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) {
        ClipData clip=
          ClipData.newUri(getContentResolver(), "A photo", outputUri);

        i.setClipData(clip);
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      }
      else {
        List<ResolveInfo> resInfoList=
          getPackageManager()
            .queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolveInfo : resInfoList) {
          String packageName = resolveInfo.activityInfo.packageName;
          grantUriPermission(packageName, outputUri,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
      }

      try {
        startActivityForResult(i, CONTENT_REQUEST);
      }
      catch (ActivityNotFoundException e) {
        Toast.makeText(this, R.string.msg_no_camera, Toast.LENGTH_LONG).show();
        finish();
      }
    }
    else {
      output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);
        Uri outputUri=FileProvider.getUriForFile(this, AUTHORITY, output);

        i.setDataAndType(outputUri, "image/jpeg");
        i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        try {
          startActivity(i);
        }
        catch (ActivityNotFoundException e) {
          Toast.makeText(this, R.string.msg_no_viewer, Toast.LENGTH_LONG).show();
        }

        finish();
      }
    }
  }
}

在这里,我在保存的实例状态为 Bundle 的状态下保持 output 的位置,因此,即使我的进程终止了,我也将获得 output 返回.

Here, I hold onto the output location in the saved instance state Bundle, so even if my process is terminated, I will get my output back.

这篇关于在Android Oreo中调用Camera Intent后,正在重新创建父级活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆