4

lets say I created an intent to pick an image from gallery or any other directory, I should get a uri from onActivityResult(), is it possible to copy the exact uri to a local file (intetnal storage).

    onActivityResult(){

     //get image uri
      Uri uri=data.getData();

      //create a file 

      File image_file=new File(path);


      //now how to save uri to image_File???


    }
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22

1 Answers1

2

Use this code to write in file

Ignore below code

File path = context.getFilesDir();
File file = new File(path, "myfile.txt");
FileOutputStream stream = new FileOutputStream(file);

Uri uri = ....
String uriStr = uri.toString();
try {
  stream.write(uriStr.getBytes());
} finally {
  stream.close();
}

Reference

Added:

get Bitmap from Uri:

   public  Bitmap getContactBitmapFromURI(Context context, Uri uri) {
    try {

        InputStream input = context.getContentResolver().openInputStream(uri);
        if (input == null) {
            return null;
        }
        return BitmapFactory.decodeStream(input);
    }
    catch (FileNotFoundException e)
    {

    }
    return null;

}

Save Bitmap to file

    public  File saveBitmapIntoSDCardImage(Context context, Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(Util.getTempFileReceivedPath(conetext));
    myDir.mkdirs();

    String fname = "file_name" + ".jpg";
    File file = new File (myDir, fname);

    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;
}
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • It's not really clear in the question, but I don't think they're trying to save the URI itself. I'm pretty sure they want the file it points to. At least, that's what the code and its comments seem to indicate. – Mike M. Mar 04 '18 at 03:00
  • yes thats why i asked again in comment but @svi.data replied about saving only uri – Abu Yousuf Mar 04 '18 at 03:02
  • I think they're using URI to mean the actual file. – Mike M. Mar 04 '18 at 03:04
  • @MikeM. because it is a bit hard to get path from uri, I want to copy uri to another file (custom file) to get a reference to it. – Hasan Bou Taam Mar 04 '18 at 03:05
  • @MikeM. lets say I picked image from gallery I want to save the exact image to another file. – Hasan Bou Taam Mar 04 '18 at 03:06
  • @AbuYousuf the code doesn't work, but thanks any way for your effort. – Hasan Bou Taam Mar 04 '18 at 03:09
  • @svi.data Yeah, that's what my suggestion addresses. If you're not getting any Exceptions with that method, then I would think that you're looking in the wrong spot for the saved file. – Mike M. Mar 04 '18 at 03:09
  • @svi.data you want to "save the exact image to another file" ? – Abu Yousuf Mar 04 '18 at 03:11
  • @MikeM. I guess I am looking at the wrong answer in the link, can you please refer to what answer you pointed? – Hasan Bou Taam Mar 04 '18 at 03:11
  • @AbuYousuf yes thats what I want to do. – Hasan Bou Taam Mar 04 '18 at 03:12
  • 2
    @svi.data In place of the `new FileOutputStream(...)` in that example, you'd use `openFileOutput()`. The file will then be under `data/data/your.package.name/`. – Mike M. Mar 04 '18 at 03:16
  • @MikeM. yeah you are right about the answer, it was a mistake done by me (I was deleting the file after it was created), thanks. If you want to provide an answer so I can accept it. – Hasan Bou Taam Mar 04 '18 at 03:21