1
  1. I am capturing image and storing it
  2. Images are getting created in specified path
  3. But I cannot display the images by obtaining from cursor

How to resolve this


How I am storing image after capturing it::

private void createFile(byte[] fileData) throws IOException {

        FileOutputStream out=null;
        try {

            //Create the directory
            String mDirectoryPath = Environment.DIRECTORY_PICTURES + LinksAndKeys.DIRECTORY_PATH_FOR_IMAGES;
            String mImageName = System.currentTimeMillis()+".jpg";
            File root = Environment.getExternalStoragePublicDirectory(mDirectoryPath);
            File dir = new File(root + File.separator);
            if (!dir.exists()) dir.mkdir();

            //Create file..
            File file = new File(root + File.separator + mImageName);
            file.createNewFile();

            out = new FileOutputStream(file);
            if(out!=null){
                out.write(fileData);
            }

        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }

    }

How I am trying to get the URI's of created images::

 private ArrayList<String> loadPhotosFromNativeGallery() {


        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;

        Cursor imagecursorExternalUri = getActivity().managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy + " DESC");

        Cursor imagecursorInternalUri = getActivity().managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy + " DESC");

        Cursor[] cursorArray = { imagecursorExternalUri, imagecursorInternalUri};

        MergeCursor mMergeCursor = new MergeCursor(cursorArray);


        ArrayList<String> imageUrls = new ArrayList<String>();

        for (int i = 0; i < mMergeCursor.getCount(); i++) {
            mMergeCursor.moveToPosition(i);
            int dataColumnIndex = mMergeCursor.getColumnIndex(MediaStore.Images.Media.DATA);
            imageUrls.add(mMergeCursor.getString(dataColumnIndex));

            System.out.println("=====> Array path => "+imageUrls.get(i));
        }


        return imageUrls;
    }
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • have you try to scan storage with media scanner for added files ? easy way is, restart your phone and check is your file exists or not (just for testing, you can't force users to restart their phone ;) ) – Shayan Pourvatan Aug 26 '17 at 09:06
  • I can see the Images created in gallery, Restarted the phone ... still images are there... Is this some thing related to read or write permission kinda thing ? – Devrath Aug 26 '17 at 09:14
  • As you can save your image into sd card I think permission hasn't problem. is there any result in `imagecursorInternalUri` and `imagecursorExternalUri`? can you retrieve other image in gallery? – Shayan Pourvatan Aug 26 '17 at 09:18
  • Yes I can see all other images ... like images i downloaded from Facebook , WhatsApp ... everything but ..... not the images I just clicked ... but its created in gallery – Devrath Aug 26 '17 at 09:19
  • try : `MediaScannerConnection.scanFile(this, new String[] { file.getPath() }, new String[] { "image/jpg" }, null);` after saving your image – Shayan Pourvatan Aug 26 '17 at 09:24
  • Its working ... After Adding that line ... What that line does ... Can you add as answer ... Ill mark as accepted :) – Devrath Aug 26 '17 at 09:40
  • why dont you use `MediaStore.Images.Media#insertImage` ? did you read the documentation? – pskink Aug 26 '17 at 10:04
  • @pskink ... Reading now :) :P – Devrath Aug 26 '17 at 10:09
  • all you need is to pass your `Bitmap`, title and description, the last two can be null (i think) – pskink Aug 26 '17 at 10:09

1 Answers1

0

The system scans the SD card when it is mounted to find any new image (and other) files. If you are programmatically adding a file, then you must scan your new file. you can use following method to do that:

MediaScannerConnection.scanFile(this, new String[] { file.getPath() }, new String[] { "image/jpg" }, null);

you can find more info from this link

you can insert your data directly with:

  ContentValues values = new ContentValues();
  values.put(MediaStore.Images.Media.TITLE, "YourTitle");
  values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
  values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
  values.put(MediaStore.MediaColumns.DATA, filePath);

  context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63