如何获取已捕获照片的uri? [英] How to get uri of captured photo?

查看:66
本文介绍了如何获取已捕获照片的uri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现什么? 我想获取捕获图像的URI并将其保存在Firebase上. 我尝试了什么? 首先,我需要打开相机.下面是我的操作方法:

What do I want to achieve? I want to get URI of the captured image and save it on Firebase. What did I try? First of all I needed to open the camera. Below how I did it:

 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null)
        startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

捕获图像后,我需要获取图像的URI.下面是我的操作方法:

After capturing the image, I needed to get the URI of the image. Below how I did it:

if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CAMERA_REQUEST_CODE) {
            if (data != null) {
                if (data.getData() != null) {
                   Uri capturedImageUri = data.getData(); 
                } else {
                    Toast.makeText(getContext(), getString(R.string.coudldnt_get_photo), Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getContext(), getString(R.string.coudldnt_get_photo), Toast.LENGTH_SHORT).show();
            }

一切都很好.但仅在某些设备上.我想它只能在超过21种设备的API级别上工作.在其他设备中,getData()返回null.

So everything was OK. But only on some devices. I suppose that it works only on API level more than 21 devices. In other devices , getData() returns null.

那么,接下来我要做什么? 我发现可以通过以下代码获取图像的位图:

So, what I have done next? I found that I can get Bitmap of the image through following code:

Bitmap bitmap = (Bitmap)data.getExtras().get("data") ; 

所以,我有图像的位图.我需要获取此图像的URI.下面是我的操作方法:

So, I have a bitmap of the image. And I needed to get the URI of this image. Below how I did it:

public Uri getImageUri(Context inContext, Bitmap inImage) {
  String path = 
  Images.Media.insertImage(inContext.getContentResolver(), inImage, 
  "Title", null);
  return Uri.parse(path);
 } 

上面的代码返回了我URI,但是图像的质量确实很差.

Above code returns me URI, but the image has really bad quality.

因此,我一直在寻找解决方案.并发现正确的方法是在启动CameraActivity时创建文件并保存该文件的uri.而onActivityResult我试图获取该uri.下面是我的操作方法:

So, I kept looking for a solution. And found that the right way is to create the file when I start the CameraActivity and save the uri of that file. And onActivityResult I tried to get that uri. Below how I did it:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {

        }
        if (photoFile != null) {
            Uri photoUri = FileProvider.getUriForFile(getContext(), "i_dont_know_what_to_write_here", photoFile);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
        }

    }

然后我的onActivityResult变成了:

    if (resultCode == Activity.RESULT_OK) {
    if (requestCode == CAMERA_REQUEST_CODE) {
        if (data != null) {
 Uri capturedPhotoUri = 
 data.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
   } else {
   Toast.makeText(getContext(), 
   getString(R.string.coudldnt_get_photo), Toast.LENGTH_SHORT).show();
            }

以上概念需要在清单中添加一些标签:

The above concept requires to add some tags on manifest :

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="i_dont_know_what_to_write_here"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/my_paths" />
        </provider>

我的xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="images"/>
</paths>

上述方法无效,因为onActivityReuslt上的data为空.

The above method didn't work because data on onActivityReuslt was null.

那么,我需要做些什么才能实现自己的目标?

So, what I need to do in order to achieve my goal?

推荐答案

请按照以下步骤操作.

步骤-1:在res/xml文件夹中创建provider_paths.xml并在其中写入以下代码.

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
</paths>

第2步:声明清单中的提供者,如下所示.

       <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

如果您在以下位置遇到错误:android:name ="android.support.v4.content.FileProvider" 使用:

If you are facing error at: android:name="android.support.v4.content.FileProvider" Use:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths" />
</provider>

步骤-3:按如下所示编写相机意图.

    Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
    Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
    m_intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(m_intent, REQUEST_CAMERA_IMAGE);

步骤-4:按如下所示在onActivityResult中处理摄像机结果.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {

        //TODO... onCamera Picker Result
        case REQUEST_CAMERA_IMAGE:
            if (resultCode == RESULT_OK) {

              //File object of camera image
                File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");

              //Uri of camera image
                Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);

            }
            break;
    }
}

这篇关于如何获取已捕获照片的uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆