【发布时间】:2020-04-28 14:52:43
【问题描述】:
我知道这已在此处多次发布,但没有一个解决方案足够清楚地说明需要做什么。我想知道我是 android 编程新手会有所帮助。
这是我在应用中创建使用相机的意图的地方。
public void captureImage(View view)
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(getPackageManager())!=null)
{
File imageFile = null;
try {
imageFile = getImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if(imageFile!=null)
{
Uri imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, IMAGE_REQUEST);
}
}
}
以下是我创建图像文件的方法,据我所知,相机图像将被临时保存。
public File getImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmsss").format(new Date());
String imageName = "jpg_"+timeStamp+"_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//storageDir.mkdirs();
perStorageDir=storageDir;
File imageFile = File.createTempFile(imageName,".jpg", storageDir);
currentImagePath = imageFile.getAbsolutePath();
return imageFile;
}
(使用currentImagePath我只能在应用程序仍在运行时访问照片)
getExternalFilesDir(Environment.DIRECTORY_PICTURES);如果我尝试在函数之外的任何位置调用它,我的应用程序就会崩溃,我不知道为什么。File.createTempFile(...)仅创建一个临时文件。我怎样才能使它成为永久添加?项目中的所有其他相关文件都已相应修改以设置权限和功能。
我要做的就是创建一个目录并在每次拍摄照片时将照片存储在其中。即使我关闭并重新打开应用程序或不拍照,我也想访问旧图像等的文件夹。
任何帮助将不胜感激!
【问题讨论】:
标签: java android android-studio android-camera android-file