我正在尝试使用此获取已下载并保存的图像的文件大小AsyncTask
protected Bitmap doInBackground(String... params) {
String urldisplay = params[0];
if(params[1] != null)
newClan = params[1];
if(params[2] != null){
Context ctx = Home.getContext();
prevClan = params[2];
File f = new File(ctx.getFilesDir(), prevClan + ".png");
if(f.exists()){
File d = f.getAbsoluteFile();
d.delete();
}
}
Bitmap emblem = null;
try {
//get emblem from url
InputStream in = new java.net.URL(urldisplay).openStream();
emblem = BitmapFactory.decodeStream(in);
} catch (MalformedURLException e) {
Log.e(e.getClass().toString(), e.getMessage());
} catch (IOException e) {
Log.e(e.getClass().toString(), e.getMessage());
}
return emblem;
}
public static void saveImage(Bitmap img){
Context ctx = Home.getContext();
try {
FileOutputStream out = new FileOutputStream(ctx.getFilesDir().getPath() + File.separatorChar + newClan + ".png");
img.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
} catch (NullPointerException e1) {
Log.e("NullPointerException DownloadImageTask", e1.toString());//TODO
}
}
这是这样称呼的
File emblemPath = new File(ctx.getFilesDir(), clan + ".png");
Log.e("image size", emblemPath.length()+"");//TODO
if(emblemPath.exists()){
emb.setImageDrawable(Drawable.createFromPath(emblemPath.toString()));
Log.e("Image exists", "size = " + emblemPath.length());//TODO
}
else{
String prevClan = pref.getString("prevClan", null);
emb.setImageBitmap(Storage.updateClanEmb(embURL, clan, prevClan));
Log.e("Image downloaded", "size = " + emblemPath.length());//TODO
}
在此之前我有一个登录屏幕,它提供所需的变量。下载的图像在ImageView
第一次显示正确但文件大小为0,注销然后重新登录后,它显示正确的文件大小。日志在这里:
- 05-25 04:52:32.973: E/result(22501): {"login":0,"clan":"Cosa Nostra","status":"0"}
- 05-25 04:52:32.984: E/Login success!(22501): Login success!
- 05-25 04:52:34.554: E/image size(22501): 0
- 05-25 04:52:35.953: E/Image downloaded(22501): size = 0
- 05-25 04:52:44.553: E/Logout(22501): Logged out
- 05-25 04:52:54.893: E/result(22501): {"login":0,"clan":"Cosa Nostra","status":"0"}
- 05-25 04:52:54.893: E/Login success!(22501): Login success!
- 05-25 04:52:57.034: E/image size(22501): 2461
- 05-25 04:52:57.094: E/Image exists(22501): size = 2461
为什么保存图像后文件大小不正确?我访问错了吗?作为旁注,我是 Android 的新手,还没有完全掌握如何使用上下文。我担心这可能是我的问题。在代码中,Home 是从中调用它的活动。
编辑:onPostExecute()
我在@Samarth 的请求中
添加了这一行
iv.setImageBitmap(result);
显示下载图像iv
的在哪里。ImageView
结果日志完全相同。有人可以解释在AsyncTask's onPostExecute()
方法中设置图像的优势吗?我似乎忘记了包含我的updateClanEmblem
方法,所以这里是:
/**
* Method to call DownloadImageTask which downloads then saves the updated clan emblem
*
* @param iv The ImageView to set the emblem to
* @param url The URL, as a String, to the page containing ONLY the clan emblem.
* @param newClan The new clan emblem to download.
* @param prevClan The old clan emblem to delete. Can be null.
*/
public static void updateClanEmb(ImageView iv, String url, String newClan, String prevClan){
String[] params = {
url,
newClan,
prevClan,
};
try {
new DownloadImageTask(iv).execute(params).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}