数据库中的毕加索图像
Picasso Image in Database
我有一个简单的问题。如何将毕加索图像(从 json 成功加载)保存到数据库中。如果可能的话,我想把它转换成位图,或者有另一种保存方式吗?
我对可绘制图像尝试了这种方法,效果很好。
我的代码:
public static byte[] imageViewToByte(ImageView image) {
Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
// Bitmap bitmap=(BitmapDrawable)image.getI);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;`.
}
将图像集成功加载到 picasso 后,使用以下代码将图像 url 转换为位图:
try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
或者,使用以下:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
通过使用sharedpreference,您可以将位图保存到内部存储器。
我有一个简单的问题。如何将毕加索图像(从 json 成功加载)保存到数据库中。如果可能的话,我想把它转换成位图,或者有另一种保存方式吗? 我对可绘制图像尝试了这种方法,效果很好。
我的代码:
public static byte[] imageViewToByte(ImageView image) {
Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
// Bitmap bitmap=(BitmapDrawable)image.getI);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;`.
}
将图像集成功加载到 picasso 后,使用以下代码将图像 url 转换为位图:
try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
或者,使用以下:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
通过使用sharedpreference,您可以将位图保存到内部存储器。