Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy image file from Android internal storage to gallery

I got a camera application where, after the user takes an image, I save it to the internal storage in a directory I have made. All that works fine, the image gets saved there and I can load it and show afterwards, but I'm having trouble saving the image to the Android gallery too.

What I want to, is after saving the image to the internal directory, copy it to the gallery.

I have tried this:

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.SIZE, file.length());
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());

    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Where the file is the image I saved in internal storage. With this method all I get is a broken image in the gallery.

like image 778
TP89 Avatar asked Sep 27 '22 12:09

TP89


1 Answers

copy it to gallery or show it in android gallery?

if you want the image show in android gallery? you can do this by scan media, this is how to scan:

Uri uri = Uri.fromFile(file); Intent scanFileIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri); sendBroadcast(scanFileIntent);

if you want just copy the file just do this :

  in = new FileInputStream(sourceFile);
  out = new FileOutputStream(destFile);
  byte[] buffer = new byte[1024];
  int read;
  while ((read = in.read(buffer)) != -1) {
    out.write(buffer, 0, read);
  }
  in.close();
  in = null;

  out.flush();
  out.close();
like image 159
ade sueb Avatar answered Oct 04 '22 03:10

ade sueb