图库和相机拍摄的照片的 URI/URL 路径不同

发布于 2024-12-03 19:33:57 字数 1392 浏览 0 评论 0原文

我试图让用户在使用设备默认相机拍照和从也默认为设备的图像库中进行选择之间进行选择。

我可以让相机拍摄照片并将其显示在应用程序中,因为它似乎喜欢直接指向 JPG 文件的 URI 路径。但是,为图库 URI 提供的路径非常不同,并且根本不显示图像。

以下是我得到的路径:

从相机拍摄时: /mnt/sdcard/filename.jpg

从图库中选择时: /external/images/media/#(我相信#是ID号/缩略图)

用于检索这两个路径的代码是:

CAMERA

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            mImageCaptureUri = 
            Uri.fromFile(new file(Environment.getExternalStorageDirectory(),
            "fname_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

GALLERY

   Intent intent = new Intent();
   intent.setType("image/*");
   intent.setAction(Intent.ACTION_GET_CONTENT);
   startActivityForResult(Intent.createChooser(intent, 
           "Complete action using"), PICK_FROM_FILE);
   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

随着画廊,它打开了,我可以很好地浏览,它只是不像拍照那样显示图像。

选择/拍摄后用于将图像拉入我的应用程序的代码是:

   ImageView getMyphoto = (ImageView) findViewById(R.id.usePhoto);
   String stringUrl = prefSettings.getString("myPic", "");
   Uri getIMG = Uri.parse(stringUrl);
   getMyphoto.setImageURI(null);
   getMyphoto.setImageURI(getIMG);

I am trying to get the users to select between taking a picture with the device default camera and select from the gallery of images also default to the device.

I can get the camera to take the picture and have it display within the app just fine since it seems to like the URI pathing straight to a JPG file. However, the pathing given to the gallery URI is very different and does not display the image at all.

Here are the pathes I get:

WHEN TAKEN FROM CAMERA:
/mnt/sdcard/filename.jpg

WHEN CHOSEN FROM GALLERY:
/external/images/media/# (# is the ID number/thumbnail I believe)

The code used for retrieving both pathes are:

CAMERA:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            mImageCaptureUri = 
            Uri.fromFile(new file(Environment.getExternalStorageDirectory(),
            "fname_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

GALLERY:

   Intent intent = new Intent();
   intent.setType("image/*");
   intent.setAction(Intent.ACTION_GET_CONTENT);
   startActivityForResult(Intent.createChooser(intent, 
           "Complete action using"), PICK_FROM_FILE);
   intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

With the Gallery, it opens and I can browse just fine, it just doesn't display the image as it does with taking the picture.

The code used for pulling the images into my app once selected/taken is:

   ImageView getMyphoto = (ImageView) findViewById(R.id.usePhoto);
   String stringUrl = prefSettings.getString("myPic", "");
   Uri getIMG = Uri.parse(stringUrl);
   getMyphoto.setImageURI(null);
   getMyphoto.setImageURI(getIMG);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-12-10 19:33:57

检查uri字符串中是否有“/external”,然后使用获取正确路径方法来获取绝对路径。

 private String getRealPathFromURI(Uri contentUri) {
        int columnIndex = 0;

        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);

        try {
            columnIndex = cursor.getColumnIndexOrThrow
                           (MediaStore.Images.Media.DATA);
        } catch (Exception e) {
            Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI",
                           Toast.LENGTH_SHORT).show();
            finish();  
            return null;
        }       
        cursor.moveToFirst();
        return cursor.getString(columnIndex);               
    }

Check for the "/external" in the uri string and then use get the right path method to get the absolute path.

 private String getRealPathFromURI(Uri contentUri) {
        int columnIndex = 0;

        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);

        try {
            columnIndex = cursor.getColumnIndexOrThrow
                           (MediaStore.Images.Media.DATA);
        } catch (Exception e) {
            Toast.makeText(ImageEditor.this, "Exception in getRealPathFromURI",
                           Toast.LENGTH_SHORT).show();
            finish();  
            return null;
        }       
        cursor.moveToFirst();
        return cursor.getString(columnIndex);               
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文