cuốn sách gpt4 ai đã làm

Android sử dụng FileProvider để chụp ảnh

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-02 22:08:02 30 4
mua khóa gpt4 Nike

Tôi đang sử dụng FileProvider để chụp ảnh trên Android Nougat, đây là mã của tôi


android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />

Đường dẫn tệp.xml:

<đường dẫn="">


Java:

 Chuỗi fileName = "máy ảnhOutput" + System.currentTimeMillis() + ".jpg";
Tệp imagePath = Tệp mới(getContext().getFilesDir(), "hình ảnh");
Tệp tệp = Tệp mới (imagePath, fileName);
Ý định cameraIntent = Ý định mới(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

đầu ra Uri cuối cùngUri = FileProvider.getUriForFile(activity, "com.mypackage.fileprovider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
getContext().grantUriPermission(
"com.google.android.GoogleCamera",
đầu raUri,
Ý định.FLAG_GRANT_WRITE_URI_PERMISSION | Ý định.FLAG_GRANT_READ_URI_PERMISSION
);
cameraIntent.setFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
hoạt động.startActivityForResult (máy ảnhIntent, ACTIVITY_REQUEST_CODE);

Hoạt động của máy ảnh bắt đầu và ảnh được chụp thành công nhưng kết quả Hoạt động không bình thường và điều duy nhất tôi thấy trong nhật ký lỗi là

CAM_StateSavePic: Đã xảy ra ngoại lệ khi lưu kết quả vào URI: Tùy chọn.of(content://com.mypackage.fileprovider/img/máy ảnhOutput1469383289530.jpg)FileNotFoundException: là một thư mục

biên tậpTệp bị thiếu#createNewFile();

câu trả lời hay nhất

Điều chính giúp URI máy ảnh và thư viện hoạt động bình thường là đường dẫn nhà cung cấp.

Bạn cần tạo một Provider_paths.xml vào thư mục /res/xml/


<đường dẫn="" xmlns:android="http://schemas.android.com/apk/res/android">


Trong tập tin danh sách của bạn, trong

Thêm dòng này vào giữa

android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths">

Còn một điều nữa mình thấy là chúng ta cần set vmPolicy trong class Application như thế này

@Ghi đè
khoảng trống công khai trênCreate() {
super.onCreate();
// Cho phép chính sách Chế độ nghiêm ngặt để hỗ trợ Nougat
Trình tạo StrictMode.VmPolicy.Builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}

Kiểm tra kỹ máy ảnh và quyền lưu trữ bên ngoài của bạn trong tệp danh sách và sau đó bạn có thể sử dụng URI kẹo dẻo.

Để biết thêm chi tiết hãy xem liên kết này:Tệp Android NUriExposeException

Về việc sử dụng FileProvider để chụp ảnh trên Android, chúng tôi tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/38555301/

30 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress