Android 11中的相机意图未响应

14

我正在使用相机意图进行工作。在 Android 10 中一切都很正常,但在 Android 11 中,结果代码为 0。

  1. 清单权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 带有文件创建的意图函数:

     private void openCameraApp()
     {
         Intent picIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).
                 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
         try {
    
             String file_path = Environment.getExternalStorageDirectory().toString() +
                     "/" + mContext.getResources().getString(R.string.app_name);
    
             File dir = new File(file_path);
             if (!dir.exists())
                 dir.mkdirs();
    
             imagePath = new File(dir, mContext.getResources().getString(R.string.app_name) + System.currentTimeMillis() + ".png");
    
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                 picIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imagePath));
                 setUri(FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, imagePath));
             } else {
                 picIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath));
                 setUri(Uri.fromFile(imagePath));
             }
    
             ((Activity) mContext).startActivityForResult(picIntent, CAMERA_REQUEST);
    
         } catch (Exception e) {
             logger.e(e);
         }
     }
    
  • 我已经在Manifest文件的应用程序标签中添加了android:requestLegacyExternalStorage="true"

    3个回答

    35

    1
    感谢您的帮助,很抱歉回复晚了。我已经尝试过这个方法,但是当我在清单文件中使用<queries>时,它会显示错误,即使我在Gradle中使用了更高的SDK级别。 - developer
    谢谢。我想知道为什么Android不会对这样的更改进行公开宣布。 - ekashking
    @ekashking,感谢您!我同意您的看法,Android 11带来了许多安全方面的改变,特别是存储处理方面。而我们有时只能在出现故障时才会知道这些改变。 - CoolMind
    我添加了相同的代码,但不起作用。https://stackoverflow.com/questions/71953359/camera-intent-not-working-proper-in-android-11-here-is-below-my-code-i-receive - Siddharth kheni
    新手们不要忘记在清单文件的应用程序标签之外添加这些查询。 - Harin Kaklotar
    显示剩余5条评论

    1

    移除这个 .putExtra(MediaStore.EXTRA_OUTPUT


    0

    activity.java 中的结果代码表示相机活动已被取消:

    /** Standard activity result: operation canceled. */
    public static final int RESULT_CANCELED    = 0;
    /** Standard activity result: operation succeeded. */
    public static final int RESULT_OK           = -1;
    /** Start of user-defined activity results. */
    public static final int RESULT_FIRST_USER   = 1;
    

    尝试启动 IMAGE_CAPTURE 活动:startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接