[英]How to save image PERMANENTLY after using camera to take photo in android?
I know this has been posted here multiple times but none of the solutions is clear enough on what needs to be done.我知道这已经在这里发布了多次,但是没有一个解决方案足够清楚地说明需要做什么。 I guess it would help to know that I'm new to android programming.我想知道我是 android 编程的新手会有所帮助。
This is where I create the intent to use camera in the app.这是我创建在应用程序中使用相机的意图的地方。
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);
}
}
}
Below is the method where I create the image file, where the camera image will be saved TEMPORARILY as I understand.下面是我创建图像文件的方法,据我所知,相机图像将被临时保存。
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;
}
(Using the currentImagePath
I can access the photo only while the app is still running) (使用currentImagePath
我只能在应用程序仍在运行时访问照片)
getExternalFilesDir(Environment.DIRECTORY_PICTURES);
crashes my app if I try to call it anywhere outside the function, I have no idea why.如果我尝试在 function 之外的任何地方调用它,我的应用程序就会崩溃,我不知道为什么。
File.createTempFile(...)
creates only a temporary file. File.createTempFile(...)
只创建一个临时文件。 How can I make that a permanent addition?我怎样才能使它成为永久添加?
All other relevant files in the project have been modified accordingly to set permissions and features.项目中的所有其他相关文件都已相应修改以设置权限和功能。
All I want to do is to create a directory and store pictures in that every time a photo is taken.我要做的就是创建一个目录并在每次拍摄照片时将照片存储在其中。 Even if I close and reopen the app or not take camera pictures, I'd like to access the folder for older images and such.即使我关闭并重新打开应用程序或不拍照,我也想访问旧图像等的文件夹。
Any help would be greatly appreciated!任何帮助将不胜感激!
If you would like to save it permanently, you can save it App-Specific Storage .如果您想永久保存它,您可以保存它App-Specific Storage 。 this is a type of storage assigned to your application package name.这是分配给您的应用程序 package 名称的存储类型。
You can find it in path data/data/ your-package-name , check the official documentation to know more about this type of storage.您可以在路径 data/data/ your-package-name中找到它,查看官方文档以了解有关此类存储的更多信息。
We should save these images in external storage as well.我们也应该将这些图像保存在外部存储中。
Step 1:步骤1:
Grant external storage permission, as you will access this storage by saving images in it.授予外部存储权限,因为您将通过在其中保存图像来访问此存储。
Step 2:第2步:
Open your camera with Intent and create a file for the taken image.使用 Intent 打开相机并为拍摄的图像创建一个文件。
private void openCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
photoFile = null;
try {
photoFile = FileHelper.createImageFile(this);
} catch (IOException ex) {
}
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"your path",
photoFile);
deleteFile(photoFile.getName());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA_RESULT);
photoFile.deleteOnExit();
}
}
}
createImageFile() is a method to create a temp file createImageFile() 是一种创建临时文件的方法
public static File createImageFile(Context context) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp;
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
;
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
Step 3:第 3 步:
Add the path of your file provider添加文件提供程序的路径
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
</paths>
In your manifest, declare this file provider path as follows:在您的清单中,声明此文件提供程序路径,如下所示:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="cyour authority"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Finally, I have a full repository on GitHub you can explore it, I think it could help you.最后,我在GitHub上有一个完整的存储库,您可以探索它,我认为它可以帮助您。
Happy Coding快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.