简体   繁体   English

MediaStore.ACTION_IMAGE_CAPTURE Output 是 0 字节

[英]MediaStore.ACTION_IMAGE_CAPTURE Output is 0 Bytes

I am having issues capturing photos on a CAT S42 running Android 11. My code worked fine on older phones so I suspect it is an Android version thing.我在运行 Android 11 的 CAT S42 上拍摄照片时遇到问题。我的代码在旧手机上运行良好,所以我怀疑它是 Android 版本的东西。 The following code creates the JPG file but it is always zero bytes:以下代码创建 JPG 文件,但它始终为零字节:

    private void startCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            File mNewPhoto = new File(getFilesDir(), "data-input-image.jpg");
            mNewPhoto.delete();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mNewPhoto));
            startActivityForResult(intent, 1);
        }
    }

I've been experimenting with using different save locations and permissions but I can't fix it.我一直在尝试使用不同的保存位置和权限,但我无法修复它。

I've got these permissions in my manifest:我的清单中有这些权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

Do not use Uri.fromFile() to get an uri.不要使用 Uri.fromFile() 来获取 uri。

Instead use FileProvider.getUriForFile().而是使用 FileProvider.getUriForFile()。

AndroidManifest.xml AndroidManifest.xml

<application>
      ...
      <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
 </application>

file_paths.xml文件路径.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="." />
    <external-path
        name="external"
        path="." />
</paths>

Java code Java代码

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String filename = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath() + "/wallpaper.jpg";
Uri fileProvider = FileProvider.getUriForFile(this, getPackageName() + ".fileProvider", new File(filename));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileProvider);
Intent chooserIntent = Intent.createChooser(cameraIntent, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{cameraIntent});
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(chooserIntent, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MediaStore.ACTION_IMAGE_CAPTURE - MediaStore.ACTION_IMAGE_CAPTURE MediaStore.ACTION_IMAGE_CAPTURE提供NullPointerException - MediaStore.ACTION_IMAGE_CAPTURE giving NullPointerException 具有action =“ MediaStore.ACTION_IMAGE_CAPTURE”的意图的类别是什么? - What is the class for the intent with action = “MediaStore.ACTION_IMAGE_CAPTURE”? Android MediaStore.ACTION_IMAGE_CAPTURE和Intent.ACTION_PICK - Android MediaStore.ACTION_IMAGE_CAPTURE and Intent.ACTION_PICK 从Intent(MediaStore.ACTION_IMAGE_CAPTURE)获取图像uri - get image uri from Intent(MediaStore.ACTION_IMAGE_CAPTURE) 应用Intent MediaStore导致应用程序崩溃 - App crashing with Intent MediaStore.ACTION_IMAGE_CAPTURE 没有为意图MediaStore调用Google GlassOnActivityResult。ACTION_IMAGE_CAPTURE - Google Glass OnActivityResult not called for intent MediaStore.ACTION_IMAGE_CAPTURE 无法将文件路径添加到MediaStore.ACTION_IMAGE_CAPTURE意向 - Trouble with adding file path to the MediaStore.ACTION_IMAGE_CAPTURE intent 如何使用自定义相机活动替换MediaStore.ACTION_IMAGE_CAPTURE? - How to replace MediaStore.ACTION_IMAGE_CAPTURE with a custom camera activity? MediaStore.ACTION_IMAGE_CAPTURE之后,onActivityResult中的位图为空 - Bitmap is null in onActivityResult after MediaStore.ACTION_IMAGE_CAPTURE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM