0

I have the following code where i am getting all the images from Camera but I want it from a certain folder as Pictures/SanPics. This folder is already created and has some images but it always returns NullPointerException.

CODE :

    final String[] columns = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
    String path = android.os.Environment
                    .getExternalStorageDirectory()
                    + File.separator
                    + "Pictures" + File.separator + "SanPics";
            File file = new File(path);
            Uri myUri = Uri.fromFile(file);
    Cursor imagecursor = getContentResolver().query(myUri, columns, orderBy, selectionArgs,null);
int image_column_index = imagecursor
                .getColumnIndex(MediaStore.Images.Media._ID);

Help will be much appreciated. Thanks in advance.

San
  • 2,078
  • 1
  • 24
  • 42
  • At which Line you got NPE?? Have you add permission of to your manifest file? – Piyush Dec 18 '13 at 12:50
  • see http://stackoverflow.com/questions/5039779/displaying-images-from-a-specific-folder-on-the-sdcard-using-a-gridview and http://stackoverflow.com/questions/16557650/how-to-display-images-saved-in-sdcard-folder-in-android – Shayan Pourvatan Dec 18 '13 at 13:04
  • @PiyushGupta I am getting NUP as soon as i reach CustomViewImages.class's onCreate() method where the above mentioned code is written. Thanks for taking the effort. And yes,i have added the permission for WRITE_EXTERNAL_STORAGE. – San Dec 18 '13 at 13:09
  • Can you tell exactly which line you got NPE??? – Piyush Dec 18 '13 at 13:09
  • @PiyushGupta I am getting the error at the last line that i just added in the edit. int image_column_index = imagecursor .getColumnIndex(MediaStore.Images.Media._ID); – San Dec 18 '13 at 13:22
  • 1
    Then `imagecursor` is `null`. I am not aware that `query()` on `ContentResolver` works for `file:///` `Uri` values. – CommonsWare Dec 18 '13 at 13:27
  • First check if your imagecursor is not null and if it is not then try to use this int image_column_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); – Piyush Dec 18 '13 at 13:31
  • you cant use contentresolver you have to use an inputstream – Daniel Bo Dec 18 '13 at 13:35
  • Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy); This line works like a charm and retrieves all the images from the camera app. Any idea on how to use inputstream instead of contentresolver. Sorry for such silly questions but i am just a beginner. Thanks for all your help. – San Dec 18 '13 at 13:45
  • i finally got what i asked for from this link http://stackoverflow.com/questions/16557650/how-to-display-images-saved-in-sdcard-folder-in-android/16557788?noredirect=1#comment30957088_16557788. Hope this helps. – San Dec 19 '13 at 05:16

0 Answers0