如何为contentprovider的投影指定隐藏文件夹

时间:2014-08-04 20:03:16

标签: android android-contentprovider mediastore

我有一个应用程序,用户必须指定要上传到服务器的图像。当用户单击按钮选择图像并从图库中指定图像时,它可以正常工作。

如果用户从隐藏文件夹中指定图像,我会得到一个NPE。我认为这是因为传递给contentprovider的投影是mediaStore,而选择的文件位于隐藏文件夹中,并且位于图库应用程序的不同文件夹中。或者我应该说uri并不存在于投影所指定的媒体商店中。

在为contentprovider创建投影时,如何在sdcard上指定隐藏文件夹而不是在mediastore中指定文件夹?

或者如果我在这里错过了这一点,我为什么要获得NPE?

提前致谢

public String getPath(Uri uri) throws Exception {

        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        // ^^^ Matt todo - this needs to work with images from all sources. In particular, 
        //it doesn't work with hidden Gallery images.
        Cursor cursor = null;
        try {
            String[] projection = { MediaStore.Images.Media.DATA };
            cursor = this.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        finally {
            try {
                cursor.close();
            }
            catch(Exception ignore) {
            }
        }
    }

08-04 20:53:58.125: D/MainActivity(10369): Single image selected: file:///storage/emulated/0/.hidden_folder/20140626_145007.jpeg
08-04 20:53:58.125: E/MainActivity(10369): Exception queueing/sending photos
08-04 20:53:58.125: E/MainActivity(10369): java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference
08-04 20:53:58.125: E/MainActivity(10369):  at uk.co.mobilewebexpert.infowrapsynclibrary.ApplicationObj.getPath(ApplicationObj.java:78)
08-04 20:53:58.125: E/MainActivity(10369):  at uk.co.mobilewebexpert.infowrapsynclibrary.ApplicationObj.addSinglePhotoToQueue(ApplicationObj.java:175)
08-04 20:53:58.125: E/MainActivity(10369):  at uk.co.mobilewebexpert.infowrapsyncapp.MainActivity.onActivityResult(MainActivity.java:337)
08-04 20:53:58.125: E/MainActivity(10369):  at android.app.Activity.dispatchActivityResult(Activity.java:5535)
08-04 20:53:58.125: E/MainActivity(10369):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3938)
08-04 20:53:58.125: E/MainActivity(10369):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
08-04 20:53:58.125: E/MainActivity(10369):  at android.app.ActivityThread.access$1300(ActivityThread.java:156)
08-04 20:53:58.125: E/MainActivity(10369):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
08-04 20:53:58.125: E/MainActivity(10369):  at android.os.Handler.dispatchMessage(Handler.java:102)
08-04 20:53:58.125: E/MainActivity(10369):  at android.os.Looper.loop(Looper.java:157)
08-04 20:53:58.125: E/MainActivity(10369):  at android.app.ActivityThread.main(ActivityThread.java:5872)
08-04 20:53:58.125: E/MainActivity(10369):  at java.lang.reflect.Method.invoke(Native Method)
08-04 20:53:58.125: E/MainActivity(10369):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
08-04 20:53:58.125: E/MainActivity(10369):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)

[EDIT1] 如果我从画廊中选择一个图像,则选择的uri是不同的。下面的uri是从我选择画廊的图像时。在这种情况下它可以正常工作。

08-04 21:37:53.285: D/MainActivity(10369): Single image selected: content://media/external/images/media/2222

1 个答案:

答案 0 :(得分:0)

解决

我刚刚注意到,在发布的第一个日志语句中,当从隐藏文件夹中选择图像时,无论如何都会返回路径,例如file:///

所以ican只需测试传入getPath()的uri。如果uri以content://开头,那么正常处理,如果它以file:///开头,那么只需返回uri.toString()