相机意图不节能的照片意图、节能、相机、照片

2023-09-03 22:39:20 作者:挑逗我你很ok

我成功地用这个code段前,但该文件指向的地方在SD卡上。

I successfully have used this code snippet before, but with the file pointing to somewhere on the SD card.

final File temp = new File(getCacheDir(), "temp.jpg");
temp.delete();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp));
startActivityForResult(intent, CONFIG.Intents.Actions.SELECT_CAMERA_PHOTO);

然而,当我使用,而不是一个LOC getCacheDir在SD卡上看来的照片永远不会保存。这是缓存目录和图像采集的限制?

However when I use getCacheDir instead of a loc on the SD card it seems the photo is never saved. Is this a limitation of cache dir and image capture?

推荐答案

从技术上说,这是因为写入内部存储不使用相机应用程序来捕捉图像时的支持。事实上,你可能会注意到印在logcat中陈述写入内部存储异常不支持。然而,真正的原因,这并不工作,是因为默认情况下将创建一个文件,该文件是专用于您的应用程序包和其他应用程序(即摄像头应用程序)不能访问该文件的位置,因为它并没有权限就这么做。外部存储文件系统的唯一的全球通俗易懂的部分。

Technically, this is because writing to internal storage is not supported when using the Camera application to capture an image. In fact, you may notice an exception printed in logcat stating Writing to internal storage is not supported. However, the real reason this doesn't work is because by default you are creating a file that is private to your application package and another application (i.e. the Camera app) can't access that file location because it doesn't have permission to do so. External storage is the only globally accessibly portion of the filesystem.

解决方法是为您打造全​​球(WORLD_WRITEABLE)权限的文件。通常情况下,这使得相机的应用程序通过传递乌里访问该文件。有没有真正的方法来做到这一点直接在文件,所以你必须创建一个使用可用的方法在上下文键,然后抓住它的控制器之后:

The workaround is for you to create the file with global (WORLD_WRITEABLE) permissions. Typically, this allows the Camera app to access the file via the passed Uri. There aren't really methods to do this directly on File, so you have to create the file using the methods available in Context and then grab a handle to it afterward:

//Remove if exists, the file MUST be created using the lines below
File f = new File(getFilesDir(), "Captured.jpg");
f.delete();
//Create new file
FileOutputStream fos = openFileOutput("Captured.jpg", Context.MODE_WORLD_WRITEABLE);
fos.close();
//Get reference to the file
File f = new File(getFilesDir(), "Captured.jpg");

这也样的限制,你可以将文件,因为上下文方法本质上创建在根文件文件目录下,你不能重定向到高速缓存目录。

This also sort of limits where you can place the file since the Context methods inherently create files in the root "files" directory, and you can't redirect that to the cache directory.

心连心