在Android应用中将相册中的图片保存到应用私有存储目录中,可以按照以下步骤进行:
1.请求权限:
在AndroidManifest.xml中添加读取和写入外部存储的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
对于Android 6.0(API级别23)及以上版本,还需要在运行时请求这些权限。
2.选择图片:
使用Intent打开相册选择图片:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
3.处理选择结果:
在onActivityResult方法中处理选择结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImageUri = data.getData();
saveImageToAppDirectory(selectedImageUri);
}
}
4.保存图片到应用私有目录:
使用ContentResolver获取图片数据并保存到应用私有目录:
private void saveImageToAppDirectory(Uri imageUri) {
try {
// 获取输入流
InputStream inputStream = getContentResolver().openInputStream(imageUri);
// 创建文件名
String fileName = System.currentTimeMillis() + ".jpg";
// 获取应用私有目录
File appDir = new File(getFilesDir(), "images");
if (!appDir.exists()) {
appDir.mkdir();
}
// 创建文件
File file = new File(appDir, fileName);
// 获取输出流
OutputStream outputStream = new FileOutputStream(file);
// 复制数据
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
// 关闭流
inputStream.close();
outputStream.close();
// 提示保存成功
Toast.makeText(this, "图片保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "图片保存失败", Toast.LENGTH_SHORT).show();
}
}
5.运行时权限请求:
在选择图片之前检查并请求权限:
private static final int REQUEST_CODE_PERMISSIONS = 100;
private boolean checkAndRequestPermissions() {
List<String> permissionsNeeded = new ArrayList<>();
final List<String> permissionsList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionsList.isEmpty()) {
if (permissionsList.size() > 0) {
permissionsNeeded.addAll(permissionsList);
}
ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE_PERMISSIONS);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 权限已授予,继续选择图片
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
} else {
Toast.makeText(this, "权限被拒绝,无法选择图片", Toast.LENGTH_SHORT).show();
}
}
}