我正在尝试将我的网格视图中选定图像的图像路径转换为uris,以便我可以将它们传递到我的照片编辑器中,并从那里进行编辑。
if (cb.isChecked()) {
String paths = imagePaths.get(i);
Uri uri = Uri.parse(paths);
editImagesUri[count] = uri;
count++;
}
那是我得到 NullPointerException 的代码,我似乎无法弄清楚问题出在哪里。
我已经对 if 语句中的内容进行了几次更改,以便它基本上做同样的事情,但错误仍然是一样的。
这是路径的值:
imagePaths = new ArrayList<String>();
imagePaths = getPathOfAllImages(getActivity());
这是uri的值:
private static Uri[] editImagesUri;
getPathOfAllImages 是我在这个网站上找到的一种方法作为答案。这是它的代码:
public static ArrayList<String> getPathOfAllImages(Activity activity) {
ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
String absolutePathOfImage = null;
String nameOfFile = null;
String absolutePathOfFileWithoutFileName = null;
Uri uri;
Cursor cursor;
int column_index;
int column_displayname;
int lastIndex;
int counter = 0;
// absolutePathOfImages.clear();
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaColumns.DISPLAY_NAME };
cursor = activity.managedQuery(uri, projection, null, null, null);
column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_displayname = cursor
.getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME);
// cursor.moveToFirst();
while (cursor.moveToNext()) {
// for(int i=0; i<cursor.getColumnCount();i++){
// Log.i(TAG,cursor.getColumnName(i)+".....Data Present ...."+cursor.getString(i));
// }
// Log.i(TAG,"=====================================");
absolutePathOfImage = cursor.getString(column_index);
nameOfFile = cursor.getString(column_displayname);
lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);
lastIndex = lastIndex >= 0 ? lastIndex
: nameOfFile.length() - 1;
absolutePathOfFileWithoutFileName = absolutePathOfImage
.substring(0, lastIndex);
if (absolutePathOfImage != null) {
absolutePathOfImageList.add(absolutePathOfImage);
}
}
// Log.i(TAG,"........Detected images for Grid....."
// + absolutePathOfImageList);
return absolutePathOfImageList;
}