如何从 Bitmap (Lollipop) 获取 Uri
How to get Uri from Bitmap (Lollipop)
好的,我试过这个代码:
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);
}
看起来它无法完成这项工作。当我记录它时,它会打印:
content://media/external/images/media/25756
这对我没有帮助,因为我 new File(Uri)
需要它。奇怪的是getImageUri方法returnsUri,但是File好像不认识。有人有方法从新制作的位图中检索 Uri 吗?
P.S。据我所知,自 KitKat 以来它不起作用。
编辑
这段代码有效,我想从中检索 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);
}
});
and looks like it can't do the job
这取决于 "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)
首先,如果要将位图保存到文件,请使用 FileOutputStream
而不是 ByteArrayOutputStream
。 Java 文件 I/O 已经存在了 ~20 年。
其次,如果您希望从像 MediaStore
这样的 ContentProvider
获取文件路径,您会失望的。你得到一个 Uri
,就像你问题中的 content://
一样。 A Uri
is not a file。使用 ContentResolver
和 openInputStream()
等方法读取 Uri
标识的数据。 "URI" 表示通过流获得的内容的不透明句柄的概念至少与 HTTP 一样长,它已经存在了约 25 年。
Strangely, getImageUri method returns Uri, but File doesn't seem to recognize it.
那是因为 Uri
不是文件。
好的,感谢@CommonsWare,我得到了它 - 做了这样的东西。希望对其他人也有帮助:
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();
}
好的,我试过这个代码:
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);
}
看起来它无法完成这项工作。当我记录它时,它会打印:
content://media/external/images/media/25756
这对我没有帮助,因为我 new File(Uri)
需要它。奇怪的是getImageUri方法returnsUri,但是File好像不认识。有人有方法从新制作的位图中检索 Uri 吗?
P.S。据我所知,自 KitKat 以来它不起作用。
编辑 这段代码有效,我想从中检索 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);
}
});
and looks like it can't do the job
这取决于 "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)
首先,如果要将位图保存到文件,请使用 FileOutputStream
而不是 ByteArrayOutputStream
。 Java 文件 I/O 已经存在了 ~20 年。
其次,如果您希望从像 MediaStore
这样的 ContentProvider
获取文件路径,您会失望的。你得到一个 Uri
,就像你问题中的 content://
一样。 A Uri
is not a file。使用 ContentResolver
和 openInputStream()
等方法读取 Uri
标识的数据。 "URI" 表示通过流获得的内容的不透明句柄的概念至少与 HTTP 一样长,它已经存在了约 25 年。
Strangely, getImageUri method returns Uri, but File doesn't seem to recognize it.
那是因为 Uri
不是文件。
好的,感谢@CommonsWare,我得到了它 - 做了这样的东西。希望对其他人也有帮助:
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();
}