I want to pick image from gallery and copy it in to other folder in SDCard.
Code to Pick Image from Gallery
Intent photoPickerIntent = new Intent(Int
Even though I have upvoted the answer by @AAnkit, I borrowed and went ahead to modify some items. He mentions to use Cursor but without proper illustration it can be confusing to newbies.
I think this is simpler than the most voted answer.
String mCurrentPhotoPath = "";
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
/*Then I proceed to select from gallery and when its done selecting it calls back the onActivityResult where I do some magic*/
private void snapOrSelectPicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(Intent.createChooser(takePictureIntent, "SELECT FILE"), 1001);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
try {
/*data.getDataString() contains your path="content://media/external/images/media/681 */
Uri u = Uri.parse(data.getDataString());
Cursor cursor = getContentResolver().query(u, null, null, null, null);
cursor.moveToFirst();
File doc = new File(cursor.getString(cursor.getColumnIndex("_data")));
File dnote = new File(mCurrentPhotoPath);
FileOutputStream fout = new FileOutputStream(dnote, false);
fout.write(Files.toByteArray(doc));
} catch (Exception e) {
e.printStackTrace();
}
}
}