我想使用JAVA将图像从ImageView保存到Android中的存储中。我想知道如何授予 API 13 及以上版本的存储权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
ActivityResultLauncher<String> requestPermission = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
// TODO
} else {
// TODO
}
});
ActivityResultLauncher<Intent> android11StoragePermission = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (isPermitted()) {
// TODO
} else {
//TODO
}
});
@TargetApi(Build.VERSION_CODES.R)
private void requestAndroid11StoragePermission() {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
android11StoragePermission.launch(intent);
}
private boolean isPermitted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
return ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
}
if (isPermitted()) {
//TODO
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestAndroid11StoragePermission();
} else {
requestPermission.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
ImageView
保存到内部存储器private void saveImage(Bitmap bitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/Cropped Images");
if (!myDir.exists()) {
myDir.mkdirs();
}
// Generate a unique file name
String imageName = "Image_" + new Date().getTime() + ".jpg";
File file = new File(myDir, imageName);
if (file.exists()) file.delete();
try {
// Save the Bitmap to the file
OutputStream outputStream;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
outputStream = Files.newOutputStream(file.toPath());
} else {
outputStream = new FileOutputStream(file);
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
// Add the image to the MediaStore
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
getApplicationContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// Trigger a media scan to update the gallery
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{file.getAbsolutePath()}, null, null);
} catch (Exception e) {
// TODO
}
}
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
saveImage(bitmap);