java 如何从 Android Activity 中的位图创建 Blob?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10618325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Create a Blob from Bitmap in Android Activity?
提问by Rami
I'm trying to create new MyImage entitly as listed in How to upload and store an image with google app engine.
我正在尝试创建新的 MyImage,如How to upload and store an image with google app engine 中所列。
Now I'm not using any Form. I have Android app that gets Uri from Gallery :
现在我没有使用任何表格。我有从 Gallery 获取 Uri 的 Android 应用程序:
m_galleryIntent = new Intent();
m_galleryIntent.setType("image/*");
m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
m_profileButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startActivityForResult(Intent.createChooser(m_galleryIntent, "Select Picture"),1);
}
});
And I'm using the Uri to create a Bitmap.
How can I create a Blob In my client from the Bitmap?
And what jars i'll have to add to my android project?
我正在使用 Uri 创建位图。
如何从位图在我的客户端中创建 Blob?
我必须将哪些 jar 添加到我的 android 项目中?
Is this a proper way to use Blob?
My main goal is to save an image uplodaed from an android in the GAE datastore, Am Using this tools properly or ther is better way?
Thatks.
这是使用 Blob 的正确方法吗?
我的主要目标是在 GAE 数据存储中保存从 android 上传的图像,是否正确使用此工具或有更好的方法?那就是。
回答by grattmandu03
You have to convert your Bitmap
into a byte[]
and after you can store it in your database as a Blob.
您必须将您的转换Bitmap
为 a byte[]
,然后才能将其作为 Blob 存储在数据库中。
To convert a Bitmap
into a byte[]
, you can use this :
要将 a 转换Bitmap
为 a byte[]
,您可以使用以下命令:
Bitmap yourBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
I hope it's what you want.
我希望这是你想要的。
回答by John smith
you can use this code :
您可以使用此代码:
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
if (bitmap!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
return null;
}
for converting bitmap
to blob.
用于转换bitmap
为 blob。
note:
笔记:
blob is an binary large object.
blob 是一个二进制大对象。