gpt4 book ai didi

android - 如何将捕获的图像文件保存到 data/data//cache ?

转载 作者:行者123 更新时间:2023-11-29 02:40:56 24 4
gpt4 key购买 nike

我需要保护我拍摄的图像。来自安卓Android Training 我能够拍摄全尺寸图像。但这不是我的需要。我需要将全尺寸图像保存到缓存目录 (/data/data//cache),当用户离开我的应用程序时,我将删除这些缓存文件夹。

这是我的代码,文件在缓存目录中成功创建。但是文件大小为零,没有写入图像

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File file=null;
try {
String fileName = "506214";
file = File.createTempFile(fileName, null, this.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
photoURI = Uri.fromFile(file);
Toast.makeText(this, photoURI.toString(), Toast.LENGTH_SHORT).show();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK){
camera_image.setImageURI(photoURI);
}
super.onActivityResult(requestCode, resultCode, data);
}

最佳答案

试试这个我的 friend

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);

并且不要忘记在 list 文件中添加 permisoon

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 如何将捕获的图像文件保存到 data/data/<package.name>/cache ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44195441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com