5

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.

TP89
  • 106
  • 1
  • 7

3 Answers3

4

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();
ade sueb
  • 261
  • 2
  • 7
  • Your scan code doesn't seem to work. What Files class are you using for the second example? Cause all I get is the mediastore.files and it doesnt have a copy method. – TP89 Oct 12 '15 at 08:44
  • sorry the second sample is use Java 7, maybe it cannot implement in Android development. The basic of copy file is read content from file source and write the content to file target. this is one example that use FileOutputStream "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();" – ade sueb Oct 12 '15 at 15:59
  • What should I use as the destFile? I tried this "File storagePath = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); out = new FileOutputStream(storagePath + "/" + System.currentTimeMillis() +".JPEG");" But the image isn't added to the galleru – TP89 Oct 14 '15 at 09:12
0

You can use this method for saving image:

  public void saveToSD(Bitmap outputImage){


            File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/"); 
            storagePath.mkdirs(); 

            File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");

            try { 
                FileOutputStream out = new FileOutputStream(myImage); 
                outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 
                out.flush();    
                out.close();
            } catch (Exception e) { 
                e.printStackTrace(); 
            }               
        }
Jas
  • 3,207
  • 2
  • 15
  • 45
  • But that requires me to have the image in the memory. At the time I want to save the image to the gallery, the image has already been saved to a file. It seems unnecessary to load the image again just to save it to another place. – TP89 Oct 12 '15 at 07:26
  • Why do you want to copy the image to gallery??? You can directly save it to sd card in OnActivityResult of your camera intent. – Jas Oct 12 '15 at 07:29
  • 1
    In my app the image gets sent to a server, and I don't want the image saved in the gallery untill I know that the server received it. From the moment the user takes the image till it gets sent to the server I go through 3 activities, which is why I already have it saved to a file. – TP89 Oct 12 '15 at 07:36
0

This is what might help :

Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));

After getting the path u can store the image by 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(imageFile, values);

Hope this helps.

Karan Khurana
  • 575
  • 8
  • 20
  • context.getContentResolver().insert(imageFile, values); takes Uri as its first parameter, and if I use Uri.fromFile(file) it throws an Unknown URL file error – TP89 Oct 14 '15 at 14:57