我使用以下代码从网址下载图片,然后保存到sqlite,然后在活动中的imageview中查看。
new LoadProfileImage().execute(jsonObject.getString("image"), id, title, promoexpdate, String.valueOf(i),flag,promostartDate);
上面的代码用于调用函数来完成上述工作。
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
String x,y,z,a,w,s;
protected Bitmap doInBackground(String... uri) {
String url = uri[0];
Log.d("ImageURL",url);
x = uri[1];
y = uri[2];
z = uri[3];
a = uri[4];
w = uri[5];
s = uri[6];
Log.d("LogValue",url+x+y+z+a+w+s);
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (IOException e) {
Log.e("ErroronImageParsing", e.getLocalizedMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if (result != null) {
int width = result.getWidth();
int height = result.getHeight();
Bitmap newBitmap = Bitmap.createScaledBitmap(result, width / 2, height / 2, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
buffer = out.toByteArray();
if (result!= newBitmap){
result.recycle();
}
Log.d("ImageUploaded", "Success");
}
try {
dbManager.open();
Cursor cursor = dbManager.fetch_PromsID(x);
if (cursor.getCount() > 0){
String fla = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PRO_FLAG));
String pri_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PRO_ID));
if (!w.equals(fla)) {
dbManager.update_Promotions(pri_ID,y,z, buffer,w,s);
}
}else {
dbManager.insertPromotions(x,y,z,buffer,w,s);
}
} catch (SQLException e) {
e.printStackTrace();
}
SqliteData();
panel.setVisibility(View.GONE);
dbManager.close();
}
}
这里,当执行以下代码时,来自URL的图像被保存到内部存储器中。我希望在保持我的意图的同时禁用自动保存。提前谢谢......
Bitmap newBitmap = Bitmap.createScaledBitmap(result, width / 2, height / 2, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
buffer = out.toByteArray();