[英]How to store image in app internal memory, after selecting from gallery and cropping through intent
在我的應用程序中,我從圖庫中選擇一個圖像,而不是裁剪該圖像,而不是將其存儲在外部存儲中。
但是,如果無法使用SD卡,則會出現問題。
因此,我在這里問您如何將圖像存儲在應用程序內部存儲中。
這是我的外部存儲代碼。
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));
}
}
您可以使用Context的getCacheDir()方法將圖像存儲在應用程序緩存目錄中。 使用以下代碼將圖像存儲在緩存目錄中。
//this will create a custom directory inside cache directory
File cacheDir = new File(this.getCacheDir(), "Custom_Directory");
if (!cacheDir.exists())
cacheDir.mkdir();
//this will give you full path of cache directory. You can use this for your file
this.getCacheDir().getAbsolutePath()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.