Android save image uri in internal storage
10,221
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();
}
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;
}
Comments
-
Hasan Bou Taam about 2 years
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??? }
-
Mike M. over 6 yearsIt'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.
-
Abu Yousuf over 6 yearsyes thats why i asked again in comment but @svi.data replied about saving only uri
-
Mike M. over 6 yearsI think they're using URI to mean the actual file.
-
Hasan Bou Taam over 6 years@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 over 6 years@MikeM. lets say I picked image from gallery I want to save the exact image to another file.
-
Hasan Bou Taam over 6 years@AbuYousuf the code doesn't work, but thanks any way for your effort.
-
Mike M. over 6 years@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.
-
Abu Yousuf over 6 years@svi.data you want to "save the exact image to another file" ?
-
Hasan Bou Taam over 6 years@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 over 6 years@AbuYousuf yes thats what I want to do.
-
Mike M. over 6 years@svi.data In place of the
new FileOutputStream(...)
in that example, you'd useopenFileOutput()
. The file will then be underdata/data/your.package.name/
. -
Hasan Bou Taam over 6 years@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.