【问题标题】:How can I convert image uri to file path in android kitkat while maintaining compatibility lower than api 19?如何在 android kitkat 中将图像 uri 转换为文件路径,同时保持低于 api 19 的兼容性?
【发布时间】:2015-09-11 13:00:00
【问题描述】:

我的应用程序选择系统相机拍摄的图像并从onActivityResult 方法获取其Uri,从这里我想将 Uri 转换为 android 标准文件路径,以便我能够通过以下方式检查其方向将文件路径传递给Exifinterface 的构造函数并执行getAttributeInt 以接收一个值,然后决定将图像旋转多少度。

我在 stackoverflow 上找到了一个示例代码 here,它能够将图像 uri 转换为文件路径。但问题是,它使用在 api 级别 19 以后添加的 DocumentContract 类,但我的应用程序需要支持低于 API 级别 19 的版本。我该怎么做?或者至少有一个替代解决方案来获取图像的方向。

【问题讨论】:

    标签: android image type-conversion uri filepath


    【解决方案1】:

    使用以下方法从 Uri 中获取文件路径。在这里,您需要传递上下文和 uri,它保持与前 Kitkat 的兼容性。

    public String getRealPathFromURI(Context context, Uri contentUri) {
    
            String res = "";
            String[] proj = { MediaStore.Images.Media.DATA };
            Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    res = cursor.getString(column_index);
                }
                cursor.close();
            } else {
                Log.d(TAG, "Cursor is null");
                return contentUri.getPath();
            }
            return res;
        }
    

    已针对相机更新:上述解决方案适用于为 Gallery Intent 返回的 Uri。对于相机意图,请使用以下代码。

    public File getOutputMediaFile() {
            // To be safe, you should check that the SDCard is mounted
            // using Environment.getExternalStorageState() before doing this.
    
            File mediaStorageDir;
            // If the externam directory is writable then then return the External
            // pictures directory.
            if (isExternalStorageWritable()) {
                mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                        .getAbsolutePath() + File.separator + IConstants.CUSTOM_PROFILE_PIC_PATH);
            } else {
                mediaStorageDir = Environment.getDownloadCacheDirectory();
            }
            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d("MyCameraApp", "failed to create directory");
                    return null;
                }
            }
            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
            File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
            return mediaFile;
        }
    

    创建一个全局变量selectedImage用于存储图片路径。

    private Uri getOutputMediaFileUri() {
        File mediaFile = Utilities.getInstance().getOutputMediaFile();
        selectedImage = mediaFile.getAbsolutePath();
        return Uri.fromFile(mediaFile);
    }
    

    现在使用以下方法调用相机意图。

    public void dispatchCameraIntent(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // If there any applications that can handle this intent then call the intent.
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                Uri fileUri = getOutputMediaFileUri();
                Log.d(TAG, "camera Uri : " + fileUri);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    
                startActivityForResult(takePictureIntent, CAMERA_PICKER);
            }
        }
    

    OnActivityResult 中使用selectedImage 作为文件路径。

    【讨论】:

    • 如果您运行的设备是 KitKat 版本,则返回 null
    • 我会试试的。谢谢顺便说一句
    • 对不起,但这对我不起作用,它返回 null。 @Kartheek 你是怎么做到的?
    • 您尝试过其他图片吗?它会为所有图像或某些图像返回 null 吗?
    • 是的,它为所有图像返回 null,因为如果返回的字符串为 null,我会尝试记录。但是这一行没有被执行:Log.d(TAG, "Cursor is null");我在 logcat 上没有找到“Cursor is null”,但返回的字符串为 null。 @Kartheek
    猜你喜欢
    • 2013-07-31
    • 2014-10-14
    • 2015-02-20
    • 1970-01-01
    • 2018-12-04
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多