【发布时间】:2014-05-14 04:35:24
【问题描述】:
我想实现一个图片选择器,因为 Recent image foler 和 Download Folder 存在错误,Android 应用在从这两者中选择图片时会崩溃文件夹,我想隐藏它们,或者直接打开 Gallery 文件夹而不显示文件夹选择器面板,
这是我的实际代码:
Intent intent = new Intent (Intent.ACTION_GET_CONTENT);
startActivityForResult (intent, 1000);
在结果方法中:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent intent) {
super.onActivityResult (requestCode, resultCode, intent);
String uri = intent.getDataString ();
String absolutePath = getRealPathFromURI (uri);
String compressedFilePath = compressImage (absolutePath);
encodedResult = base64File (new File (compressedFilePath)); //String
}
protected String getRealPathFromURI (String contentURI, UIView uiView) {
Uri contentUri = Uri.parse(contentURI);
Cursor cursor = ((ContextWrapper) uiView).getContentResolver ().query(contentUri, null, null, null, null);
if (cursor == null) {
return contentUri.getPath();
} else {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaColumns.DATA);
return cursor.getString(index);
}
}
因此,当我从以下文件夹中选择图像时:图库、图片等...它工作得很好。但是对于 2 个文件夹:Download 和 Recent 它崩溃并给了我:
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
或者也许有一种我不知道的更正确的方法?
您对此有任何想法吗?我怎样才能做到这一点?谢谢。
【问题讨论】:
-
您的 Android 应用崩溃了?为什么会这样?
-
同意。我不会为您的用户减少功能 - 并且以跨设备不兼容的方式 - 我会专注于为您现有的问题寻求帮助。
-
谢谢。是的,我同意,这更明智。我已经用更多细节更新了我的问题。