I am looking for someone to assist in the code i require in my application to copya image from where they get stored on the HTC desire as standard(the gallery) to a another folder on the SD card. I want the user to be able to click on a button and a certain file is copied from the SD card gallery folder to the another folder on the SD card? Thanks
Asked
Active
Viewed 1.7k times
2 Answers
27
Usmaan,
You can launch the gallery picker intent with the following:
public void imageFromGallery() {
Intent getImageFromGalleryIntent =
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE);
}
When it returns, get the path of the selected image with the following portion of code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode) {
case SELECT_IMAGE:
mSelectedImagePath = getPath(data.getData());
break;
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
startManagingCursor(cursor);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Now that you have the pathname in a string you can copy it to another location.
Cheers!
EDIT: If you just need to copy a file try something like...
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String sourceImagePath= "/path/to/source/file.jpg";
String destinationImagePath= "/path/to/destination/file.jpg";
File source= new File(data, sourceImagePath);
File destination= new File(sd, destinationImagePath);
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {}

Nouman Ghaffar
- 3,780
- 1
- 29
- 37

Will Tate
- 33,439
- 9
- 77
- 71
-
Sorry i dont understand, i know hot to take a photo and etc within my application....but later on i want to copy the image from the gallery folder to a different folder,im not sure this code does this does it? – Beginner Feb 07 '11 at 14:10
-
The first portion of this code will launch you gallery and allow you to select an image. Once you select the image it will call `onActivityResult()` from the second portion of the code I posted. It will take the data returned from the gallery app and the `getPath()` function i posted will return to you the full path of the selected image as a String. This code won't allow you to take a picture with the camera. – Will Tate Feb 07 '11 at 14:17
-
i want to copy the image to a different folder..Also i dont really want to open the gallery, i know the image name already and i know on the sd card it is stored sdcard/dcim/imagename – Beginner Feb 07 '11 at 14:20
-
looking for somthing that like says copy sdcard/dcim/image.jpg to sdcard/newfolder – Beginner Feb 07 '11 at 14:22
-
oh...you just need the actual copy? haha sorry i posted everything leading up to that, sorry > – Will Tate Feb 07 '11 at 14:26
-
yeh i just want the user to click on button which copies an image from sdcard/dcim folder to just sdcard/newfolder – Beginner Feb 07 '11 at 14:28
-
Added file copy code of two known files to my original answer – Will Tate Feb 07 '11 at 14:36
-
you may need to do a destination.mkdir() if the destination directory doesn't already exist. It may not work just cut/paste but its a general outline of a file copy process. – Will Tate Feb 07 '11 at 14:49
-
Try just `File source= new File(souceImagePath);` without the `data` variable – Will Tate Feb 07 '11 at 14:57
-
used Environment.getExternalStorageDirectory() for both and works thanks mate – Beginner Feb 07 '11 at 14:58
1
Gallery images are already stored in the SD card in Android phones. The official documentation has a nice section on working with external storage, which you should check out.

goncalossilva
- 1,830
- 15
- 25