Reputation: 4343
Ok, I tried this code:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
and looks like it can't do the job. When I log it it prints:
content://media/external/images/media/25756
which doesn't helps me because I need it for new File(Uri)
. Strangely, getImageUri method returns Uri, but File doesn't seem to recognize it. Anyone has method to retrieve Uri from freshly made bitmap?
P.S. As far as I read, it doesn't work since KitKat.
edit This code works and from this I want to retrieve Uri:
croppedBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
cropImageView = (CropImageView) findViewById(R.id.CropImageView);
cropImageView.setImageBitmap(croppedBitmap);
Button crop = ...;
crop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cropContainer.setVisibility(View.GONE);
mImageView.setVisibility(View.VISIBLE);
Bitmap croppedImage = cropImageView.getCroppedImage();
mImageView.setImageBitmap(croppedImage);
paramsContener.setVisibility(View.VISIBLE);
}
});
Upvotes: 0
Views: 1712
Reputation: 4343
Okay, I got it thanks to @CommonsWare - made something like this. Hope will help it another people as well:
public String makeBitmapPath(Bitmap bmp){
// TimeStamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "Photo"+ timeStamp +".jpg"); // the File to save to
try {
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (IOException e){
// whatever
}
return file.getPath();
}
Upvotes: 1
Reputation: 1006614
and looks like it can't do the job
That would depend upon what "the job" is.
When I log it it prints:
content://media/external/images/media/25756
which doesn't helps me because I need it for new File(Uri)
First, if you want to save a bitmap to a file, use a FileOutputStream
rather than a ByteArrayOutputStream
. Java file I/O has been around for ~20 years.
Second, if you are expecting to get file paths from a ContentProvider
like MediaStore
, you are going to be disappointed. You get back a Uri
, like the content://
one in your question. A Uri
is not a file. Use a ContentResolver
and methods like openInputStream()
to read in the data identified by that Uri
. The concept of a "URI" representing an opaque handle to something you get via a stream has been around at least as long as has HTTP, which has been around for ~25 years.
Strangely, getImageUri method returns Uri, but File doesn't seem to recognize it.
That is because a Uri
is not a file.
Upvotes: 1