我使用此代码下载图像
public void buttonCondivisione(View view) {
//onShareItem(view);
String url = Utilities.removeString(mImages[POSITION_IMAGE].getLink());
new DownloadImage().execute(url);
}
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 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();
Toast.makeText(CategoryActivity.this, "FATTO", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.d("6SENSE_APP Save Image", "Exception 2, Something went wrong!");
Toast.makeText(CategoryActivity.this, "No FATTO", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
但我无法在下载后找到图片。一切都好,因为我在几秒钟后看到Toast消息。我尝试不同的方式下载我的图像,但每种方法都不适合我。 Stackoverflow是我的最后一次机会。