简体   繁体   English

来自Android相机意图的图像路径

[英]Image path from Android camera intent

I am trying to figure out the right way to get a file path from the camera after a picture is taken: 我正在尝试找出在拍照后从相机获取文件路径的正确方法:

Launch the camera intent. 启动相机意图。 Since I am telling the camera to write to internal storage give it the uri and read permission: 由于我告诉相机写入内部存储,因此请给它提供uri和读取权限:

File file = new File(context.getFilesDir(), "picture.jpg");
Uri uri = FileProvider.getUriForFile(getApplicationContext(), "my.app.package.fileprovider", file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, ACTION_IMAGE_CAPTURE_REQUEST_CODE);

Listen for the camera intent result: 监听相机意图结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
        return;
    }
    switch (requestCode) {
    case ACTION_IMAGE_CAPTURE_REQUEST_CODE:
        // How do I get the local path to the file here?
        break;
    }
}

What is the best way to get the file path on camera intent return. 在相机意图返回上获取文件路径的最佳方法是什么。 Sure I can save off the file path to a member variable before launching the intent, but that seems bad, seems I should get the path from the onActivityResult. 当然,我可以在启动该意图之前将文件路径保存为成员变量,但这似乎很糟糕,似乎我应该从onActivityResult获取路径。

I have tried this ( Get Image path from camera intent ): 我已经尝试过这个( 从相机意图中获取图像路径 ):

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI,projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();

String imagePath = cursor.getString(column_index_data);

Only difference being that I am using INTERNAL_CONTENT_URI as I am trying to store the pic internally. 唯一的不同是,我在尝试内部存储图片时使用的是INTERNAL_CONTENT_URI。 If I do this I get an exception: 如果我这样做,我会得到一个例外:

Error getting data column 获取数据列时出错
java.lang.IllegalArgumentException: column '_data' does not exist java.lang.IllegalArgumentException:列_data不存在

Am I going about this wrong. 我要解决这个错误吗? I want to take a pic and store that pic in internal storage. 我想拍摄一张照片并将该照片存储在内部存储器中。

EDIT: 编辑:

One more thought. 再想一想。 Should I be storing pics internally? 我应该在内部存储照片吗? I am right not because of the new android M permissions. 我是对的,不是因为新的android M权限。 I already have to ask the user for Camera permission, if I store pic to external storage, I have to ask the user for permission to write to to external. 我已经必须向用户请求“相机”权限,如果我将照片存储到外部存储,则必须向用户授予写到外部文件的权限。 Lastly if I store the image externally, it is readable by all applications on the device that can read from external storage. 最后,如果我将图像存储在外部,则设备上所有可以从外部存储读取的应用程序都可以读取该图像。 This may be a privacy issue for my app. 这可能是我的应用程序的隐私问题。

What is the best way to get the file path on camera intent return 什么是获取相机意图返回文件路径的最佳方法

You specified the file path in EXTRA_OUTPUT . 您在EXTRA_OUTPUT指定了文件路径。 Either the camera put the photo there, or it didn't. 相机将照片放在那里,或者没有。 If it did, then you already know the file path: new File(context.getFilesDir(), "picture.jpg") . 如果是这样,那么您已经知道文件路径: new File(context.getFilesDir(), "picture.jpg") That should be the case the vast majority of the time. 绝大多数时候应该是这种情况。

Some camera apps are buggy and will ignore EXTRA_OUTPUT . 某些相机应用有EXTRA_OUTPUT ,会忽略EXTRA_OUTPUT In those cases: 在这些情况下:

  • You can see if the camera app returned a Uri in the Intent passed to onActivityResult() , then use ContentResolver and openInputStream() to begin the process of copying the image to your desired location, or 您可以查看相机应用程序是否在传递给onActivityResult()Intent返回了Uri ,然后使用ContentResolveropenInputStream()开始将图像复制到所需位置的过程,或者

  • You can see if the "data" extra exists, in which case that's a Bitmap thumbnail that you can save to your desired location. 您可以查看是否额外存在"data" ,在这种情况下,这是Bitmap缩略图,您可以将其保存到所需的位置。

In all of these cases, the file path is what you specified; 在所有这些情况下,文件路径就是您指定的路径。 it is merely a question of whether or not it takes additional work for you to get the image there. 这仅仅是一个问题,您是否需要付出额外的努力才能使图像在那里。

Sure I can save off the file path to a member variable before launching the intent, but that seems bad 当然可以在启动Intent之前将文件路径保存到成员变量中,但这似乎很糟糕

I have no idea why you would think that. 我不知道你为什么会这样想。

seems I should get the path from the onActivityResult 似乎我应该从onActivityResult获取路径

You are welcome to believe whatever you want. 欢迎您相信任何想要的东西。 Camera app developers are hopefully reading the documentation for ACTION_IMAGE_CAPTURE . 相机应用程序开发人员希望阅读ACTION_IMAGE_CAPTURE的文档 That documentation does not state that the camera app has to return anything if you provide EXTRA_OUTPUT . 该文档没有说明如果您提供EXTRA_OUTPUT ,则相机应用程序必须返回任何EXTRA_OUTPUT Hence, many camera apps will not return anything. 因此,许多相机应用程序将不会返回任何内容。


UPDATE based on edit: 基于编辑的更新

Should I be storing pics internally? 我应该在内部存储照片吗?

Um, you already are. 嗯,你已经是。

if I store pic to external storage, I have to ask the user for permission to write to to external 如果我将图片存储到外部存储中,则必须请求用户允许写入外部文件

That depends on where you are writing. 那要看你在哪里写。 getExternalFilesDir() , getExternalCacheDir() , and kin from Context do not require a permission on Android 4.4+. getExternalFilesDir() getExternalCacheDir()并从亲戚Context并不需要在Android 4.4+权限。 The methods you call on Environment for external storage locations (eg, getExternalStoragePublicDirectory() ) do require WRITE_EXTERNAL_STORAGE , which is a dangerous permission and would need to be requested at runtime. 您在Environment为外部存储位置调用的方法(例如, getExternalStoragePublicDirectory() )确实需要WRITE_EXTERNAL_STORAGE ,这是一个dangerous权限,需要在运行时进行请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM