0

I am using Android 7.0 mobile device for my testing.

I am implementing an android app. I need to take a photo from device camera and just select it from camera folder/ Gallery using by the application.

But after taking a photo and go back to my application and try to select that captured photo by my app, when moving to camera folder, the recently taken photo is not in the gallery.

But I noticed when I take another photo and go to select that photo from again via my app, from the camera folder, I can see the photo I took previously but not the recently taken.

I followed this answer - Android: Refreshing the Gallery after saving new images. But it does not work for me.

If somebody can reply with the answer and that answer contains with a file path to find, please add those details also. ex:- "how to take android camera image gallery path"

The code I use here:-

    val cursor: Cursor? = contentResolver.query(uri, projectionColumns, null, null, sortOrder)
    if (cursor != null && cursor.moveToFirst()) {
        while (cursor.moveToNext()) {
            // Get values per item
            val imageId = cursor.getString(cursor.getColumnIndex(projectionColumns[0]))
            val imageName = cursor.getString(cursor.getColumnIndex(projectionColumns[1]))
            val imagePath = cursor.getString(cursor.getColumnIndex(projectionColumns[2]))
            val dateTaken = cursor.getString(cursor.getColumnIndex(projectionColumns[3]))
            val imageSize = cursor.getString(cursor.getColumnIndex(projectionColumns[4]))
            val bucketId = cursor.getString(cursor.getColumnIndex(projectionColumns[5]))
            val bucketName = cursor.getString(cursor.getColumnIndex(projectionColumns[6]))

1 Answers1

0

please note that if you are giving the intent a Uri or location somewhere in external or internal private directory to store the image picture after it gets clicked then you need to broadcast it manually or try to move it to another folder.

here is code to broadcast it by passing the path of the file.

public void broadCastMedia(Context context, String path) {
    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File file = new File(path);
    intent.setData(Uri.fromFile(file));
    context.sendBroadcast(intent);
}

and here is how to copy one to another location say pictures directory then you need to create a temp file to pictures directory and an original file and pass to this.

//temp file
    File destFile = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "image.jpg");
//original file    
    File sourceFile = new File(uri.getPath());

     public static Boolean copyFile(File sourceFile, File destFile)
          throws IOException {
        if (!destFile.exists()) {
          destFile.createNewFile();

          FileChannel source = null;
          FileChannel destination = null;
          try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
          } finally {
            if (source != null)
              source.close();
            if (destination != null)
              destination.close();
          }
          return true;
        }
        return false;
      }

and if you have given EXTRA_OUTPUT in intent then you need to store the Uri you have passed as extra as data intent in onActivityResult() will be null at that time you can use the stored Uri to perform any operation on the stored file.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52