Android 将图像从照相机保存到应用程序专用存储
这里有很多关于如何在android外部存储器上创建文件的信息,将uri传递给ACTION_IMAGE_CAPTURE,然后将图像保存在那里 但是,我想在应用程序的私有存储(有时称为内部存储)中创建一个文件,并将该文件uri传递给ACTION_IMAGE_CAPTURE,但Camera intent会返回onActivityResult中取消的结果 我该怎么办Android 将图像从照相机保存到应用程序专用存储,android,android-intent,android-camera,android-camera-intent,Android,Android Intent,Android Camera,Android Camera Intent,这里有很多关于如何在android外部存储器上创建文件的信息,将uri传递给ACTION_IMAGE_CAPTURE,然后将图像保存在那里 但是,我想在应用程序的私有存储(有时称为内部存储)中创建一个文件,并将该文件uri传递给ACTION_IMAGE_CAPTURE,但Camera intent会返回onActivityResult中取消的结果 我该怎么办 private void checkWhetherCameraIsAvailableAndTakeAPicture() { //
private void checkWhetherCameraIsAvailableAndTakeAPicture() {
// Check wether this device is able to take pictures
if (PhotoHandler.isIntentAvailable(a, MediaStore.ACTION_IMAGE_CAPTURE)) {
System.gc();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imageFile = null;
try {
if(a.application.user.isPublishImagesToGallery()){
imageFile = a.application.photoHandler.createImageFileOnExternalStorage();
} else {
imageFile = a.application.photoHandler.createImageFileOnInternalStorage();
}
imagePath = imageFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
} catch (IOException e) {
e.printStackTrace();
imageFile = null;
imagePath = null;
}
startActivityForResult(takePictureIntent, Preferences.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA);
} else { // Notify the user that their device is unable to take photos
a.application.toastMaker.toastLong(ErrorMessages.DEVICE_UNABLE_TO_TAKE_PHOTOS);
}
} // End of checkWhetherCameraIsAvailableAndTakeAPicture
public File createImageFileOnInternalStorage() throws IOException {
return createTempFileOnGivenLocation();
}
private String imageFilename() {
return filenamePrefix + Calendar.getInstance().getTimeInMillis();
}
private File createTempFileOnGivenLocation() throws IOException {
File imageFile = File.createTempFile(imageFilename(), filenameSuffix, context.getFilesDir());
setImagePathTemporary(imageFile.toString());
return imageFile;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Preferences.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA) {
if (resultCode != Activity.RESULT_CANCELED) {
handlePhotoTakenWithCamera();
} else {
// ONACTIVITYRESULT Returns Here!
new File(imagePath).delete())
}
} else if (requestCode == Preferences.REQUEST_CODE_PICK_IMAGES_FROM_GALLERY) {
if (resultCode != Activity.RESULT_CANCELED) {
handlePhotosPickedFromGallery(data);
}
}
} // End of onActivityResult
每个应用程序的内部存储都是私有的。摄像头应用程序无法访问应用程序的内部存储。在内部存储器中获取映像的最佳方法是 1) 在应用程序内使用自己的相机拍摄图像 或
2) 使用相机拍摄图像,以捕获将图像保存到外部存储器的图片
onActivityResult
将图像复制到应用程序的内部存储,并从外部存储中删除。可在此答案中找到复制文件的代码 如中所述,摄像头应用程序无法直接写入应用程序的内部存储器
不过,您可以编写一个应用程序,它将能够以流的形式使用摄像头应用程序中的数据,并自己保存
这是如何让摄像头应用程序知道将文件放在何处
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.withAppendedPath(CONTENT_URI_OF_YOUR_CONTENT_PROVIDER, "id_of_your_image");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
如何构建真正的内容提供商,我觉得有点超出了这个问题的范围。阅读上面链接的文档并搜索相关问题。
一个提示是,您必须实现该方法才能做到这一点。
它可能看起来像这样
public Uri insert(Uri uri, ContentValues values) {
String name = uri.getPath();
File file = new File(name);
OutputStream stream = new FileOutputStream(file);
byte[] data = (byte[]) values.get(KEY_FILE_CONTENT);
try {
stream.write(data);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
return uri;
}
但是由于内容提供商有多复杂,上面的代码可能不适合你。@J.K.太好了。祝你好运。复制文件的代码可以在这个答案中找到@J.K.我编辑了这个问题并提供了一些进一步的信息。顺便说一句,我的方法比上一个答案的方法要好。您不必自己复制文件,相机会通过内容提供商将其直接写入内部存储器。(这也意味着图像可能永远不会存储在公共目录中-安全:)-但这实际上取决于摄像头应用程序本身)非常感谢。如果可以的话,我会试试的