在我的应用程序中,我从图库中选择图像,而不是裁剪该图像,而不是将其存储在外部存储中。
但是如果 sd-card 不可用,就会出现问题。
所以在这里我问你如何将图像存储在应用程序内部存储中。
这是我的外部存储代码。
Override
public void onClick(View v) {
int outputX = 400;
int outputY = 400;
int aspectX = 1;
int aspectY = 1;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", outputX);
intent.putExtra("outputY", outputY);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
selectedImageUri = getTempUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, 2);
}
以下是我获取外部文件 URI 的方法:
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
private File getTempFile() {
if (isSDCARDMounted()) {
File f = new File(Environment.getExternalStorageDirectory(),
TEMP_PHOTO_FILE);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(MainActivity.this, "file issue", Toast.LENGTH_LONG)
.show();
}
return f;
} else {
return null;
}
}
private boolean isSDCARDMounted() {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 2) {
path = selectedImageUri.getPath();
imageView.setImageBitmap(BitmapFactory
.decodeFile(path));
}
}