我有一个带有图像网址的字符串。
有人可以给我代码来实现吗?
以前所做的一切都不起作用。
到目前为止代码:
dlbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadImage().execute(id);
}
});
public void saveImage(Context context, Bitmap b, String imageName)
{
FileOutputStream foStream;
try
{
foStream = context.openFileOutput(imageName, Context.MODE_PRIVATE);
b.compress(Bitmap.CompressFormat.PNG, 100, foStream);
foStream.close();
}
catch (Exception e)
{
Log.d("saveImage", "Exception 2, Something went wrong!");
e.printStackTrace();
}
}
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
private String TAG = "DownloadImage";
private Bitmap downloadImageBitmap(String sUrl) {
Bitmap bitmap = null;
try {
InputStream inputStream = new URL(sUrl).openStream(); // Download Image from URL
bitmap = BitmapFactory.decodeStream(inputStream); // Decode Bitmap
inputStream.close();
} catch (Exception e) {
Log.d(TAG, "Exception 1, Something went wrong!");
e.printStackTrace();
}
return bitmap;
}
@Override
protected Bitmap doInBackground(String... params) {
return downloadImageBitmap(params[0]);
}
protected void onPostExecute(Bitmap result) {
saveImage(getApplicationContext(), result, "my_image.png");
}
}
public Bitmap loadImageBitmap(Context context,String imageName){ Bitmap bitmap = null; FileInputStream fiStream; 尝试{ fiStream = context.openFileInput(imageName); bitmap = BitmapFactory.decodeStream(fiStream); fiStream.close(); } catch(例外e){ Log.d(“saveImage”,“例外3,出错了!”); e.printStackTrace(); } 返回位图; }
但没有任何反应。 没有在图库中存储的图像,也不会显示toast