【发布时间】:2015-02-23 12:01:41
【问题描述】:
我创建了一个应用程序,允许用户从图库中选择图像或从相机拍照并将该图像上传到网络服务器。此代码工作正常。现在在其他屏幕上我正在从网络服务器下载图像并将其存储在 sd 卡中。如果从图库中选择图像,则图像将显示在图像视图中,但是如果从相机捕获图像,则即使文件中的图像也不会显示在图像视图中存在于 SD 卡中
显示图像并从服务器下载的代码
private static class DownloadImage extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String filePath = downloadFile("my web service");
return filePath;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result.equalsIgnoreCase("")) {
ivProfilePic.setImageDrawable(context.getResources().getDrawable(R.drawable.user_default));
progressBar.setVisibility(View.GONE);
} else {
profilePicPath = result;
Bitmap bitmapProfilePic = BitmapFactory.decodeFile(profilePicPath);
ivProfilePic.setImageBitmap(bitmapProfilePic);
progressBar.setVisibility(View.GONE);
}
}
}
public static String downloadFile(String url, String dest_file_path) {
try {
File dest_file = new File(dest_file_path);
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file));
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
return "";
} catch (IOException e) {
return "";
}
return dest_file_path;
}
【问题讨论】:
-
我可以看到从相机拍照并显示到图像视图的代码吗?
标签: android image bitmap android-imageview