3

So far what i have achieved is that i can store Image clicked from camera to a new folder

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        f = new File(Utils.getPath(), new Date().getTime() + ".jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(intent, TAKE_PHOTO);

But I dont know how to store an Image selected from gallery to the same folder i created. Please help me. Thank you in advance.

AmeyaG
  • 176
  • 1
  • 1
  • 10
  • 3
    you are create folder of your project name and you just copy the capture image into your project name folder???? want you want first clarify.. – Ricky Patel Apr 07 '16 at 06:08
  • this topic may help you: http://stackoverflow.com/questions/11846108/android-saving-bitmap-to-sd-card – Bui Quang Huy Apr 07 '16 at 06:10
  • The Utils.getPath() returns the location where i want to create a new folder to store the image. Rest is handled by intent. But MediaStore.EXTRA_OUTPUT doesn't work when you are selecting an image from gallery. So how to store the image selected from gallery to another folder ? – AmeyaG Apr 07 '16 at 06:12

2 Answers2

6

First, get real path from URI you got from gallery.

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 copy image to another location,

 private void copyFile(File sourceFile, File destFile) throws IOException {
            if (!sourceFile.exists()) {
                return;
            }

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



  File destFile = new File("Dest path");

  copyFile(new File(getPath(data.getData())), destFile);

check out urls for more details,

  1. How to Copy Image File from Gallery to another folder programmatically in Android

  2. Android copy image from gallery folder onto SD Card alternative folder

Community
  • 1
  • 1
Rakesh
  • 142
  • 2
0

The solution above works for me but with minor changes:

The Image clicked by the camera is saved by the ACTION_IMAGE_CAPTURE intent. Whereas for picking an image from gallery for our own app, we need to save the Image coming from ACTION_PICK intent. Saving in this case is basically copying image from gallery to your own app folder after showing it in your ImageView.

First you need to have the destination file: So call the following method to get the directory of your app which is in Android>data>YOUR APP FOLDER>Files>Pictures:

private File mPhotoFile;
mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto);

Here is the getPhotoFile() Function

public File getPhotoFile(Photo photo){
    File externalFilesDir=mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);//getExternalStorageDirectory().toString());

    if(externalFilesDir==null){
        return null;
    }
    return  new File(externalFilesDir, photo.getPhotoFilename());
}

Now you have the file where you want to save the image picked by Image Intent. Now call the ACTION_PICK intent .

private static final int PICK_IMAGE_REQUEST=2;
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  //Trying intent to pick image from gallery
                    pickIntent.setType("image/*");
                    Uri uriImagePath = Uri.fromFile(mPhotoFile);
                    pickIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriImagePath);
                    startActivityForResult(pickIntent, PICK_IMAGE_REQUEST);

After receiving the result in onActivityResult Following is my code:

   if(requestCode==PICK_IMAGE_REQUEST){
        Picasso.with(getActivity()).load(data.getData()).fit().into(mPhotoView);
        File file=getBitmapFile(data);


        try {
           copyFile(file, mPhotoFile);
        }catch(IOException e){
          Toast.makeText(getActivity(),"Cannot use Gallery, Try Camera!",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

After Getting the data, I passed it onto the next method to find the absolute path of the picked image by Intent.ACTION_PICK. Following is the method definition:

public File getBitmapFile(Intent data){
    Uri selectedImage=data.getData();
    Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
    cursor.moveToFirst();

    int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    String selectedImagePath=cursor.getString(idx);
    cursor.close();

    return new File(selectedImagePath);

}

After getting the absolute path of the picked image. I called the copyFile() function. Which is as follows. Remember that I already generated my new file path.

public File getBitmapFile(Intent data){
    Uri selectedImage=data.getData();
    Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
    cursor.moveToFirst();

    int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    String selectedImagePath=cursor.getString(idx);
    cursor.close();

    return new File(selectedImagePath);

}

The Destination for me was mPhotoFile. That we generated first. This whole code will work if you want to pick image from gallery and store it to further show in the imageView of your app every time you open it.

Shubhdeep Singh
  • 407
  • 6
  • 7