将图像路径转换为uris

时间:2013-01-13 03:37:21

标签: android arrays if-statement uri android-image

我正在尝试将网格视图中所选图像的图像路径转换为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;
    }

0 个答案:

没有答案
相关问题