java - Android KitKat 图像选择不返回任何东西

标签 java android return android-image android-4.4-kitkat

我一直在尝试获取在 KitKat 画廊中选择的图像的绝对图像路径,但它似乎没有成功。无论我做什么,我的变量 IMAGE_FILEPATH 总是“”。这是我的 onActivityResult()

的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;
    if (null == data) return;
    Uri originalUri = null;
    if (requestCode == 1) {
        //JB!!!
            Uri uri = data.getData();

            if (uri != null) {

                try {
                    // User had pick an image.
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursor cursor = getContentResolver()
                            .query(uri,
                                    filePathColumn, null, null, null);

                    cursor.moveToFirst();

                    IMAGE_FILEPATH = cursor.getString(0);
                    cursor.close();
                } catch (Exception e) {
                    Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
                }
            }
    } else if (requestCode == 2) {
        //KK!!!
            Uri uri = data.getData();

            if (uri != null) {
                try {
                    if( uri == null ) {
                        IMAGE_FILEPATH = uri.getPath();
                    } else {
                        String[] projection = { MediaStore.Images.Media.DATA };
                        Cursor cursor = managedQuery(uri, projection, null, null, null);
                        if( cursor != null ){
                            int column_index = cursor
                                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                            cursor.moveToFirst();
                            IMAGE_FILEPATH =  cursor.getString(column_index);
                        } else {
                            IMAGE_FILEPATH = uri.getPath();
                        }
                    }


                } catch (Exception e) {
                    Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
                }
            }
    }
    Resource.toast(IMAGE_FILEPATH);

    super.onActivityResult(requestCode, resultCode, data);
}

怎么了?我尝试了多种解决方案,但似乎没有任何效果。

最佳答案

在 KitKat 中,Gallery 返回这样的 URI:content://com.android.providers.media.documents/document/image:1

代替:

content://media/external/images/media/1

因此,您可以在 KK 下编写以下内容以使其工作:

if (uri != null) {
    try {
        if( uri == null ) {
            IMAGE_FILEPATH = uri.getPath();
        } else {
            // get the id of the image selected by the user
            String wholeID = DocumentsContract.getDocumentId(data.getData());
            String id = wholeID.split(":")[1];

            String[] projection = { MediaStore.Images.Media.DATA };
            String whereClause = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = getContentResolver().query(getUri(), projection, whereClause, new String[]{id}, null);
            if( cursor != null ){
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    IMAGE_FILEPATH = cursor.getString(column_index);
                }

                cursor.close();
            } else {
                IMAGE_FILEPATH = uri.getPath();
            }
        }
    } catch (Exception e) {
        Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
    }
}

以及我使用的函数:

private Uri getUri() {
    String state = Environment.getExternalStorageState();
    if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
    }

    return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}

这些帖子对我有帮助:retrieve absolute path when select image from gallery kitkat androidGet real path from URI, Android KitKat new storage access framework

关于java - Android KitKat 图像选择不返回任何东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22237028/

相关文章:

java - 从字符串中删除停用词

android - android :scaleType ="fitCenter" only work with fix Layout_width and Layout_height attributes?

c++ - 使用对(成员)函数返回的对象的引用是否安全甚至可能?

android - Lastpass 自动填充在 Android 中如何工作?

android - XMPP - 资源绑定(bind)有用性

c++ - 使用 "?:"运算符返回

c - 在 FreeBSD/bin/and/usr/bin 中返回 vs 退出

java - 如何在此 ArrayList 中搜索 null,然后将其替换为另一个对象?

java - 反序列化一个 ArrayList。没有有效的构造函数

java - 从不同的类调用方法并保持非静态