问题描述
我创建了一个应用程序,我允许用户从图库中选择图像或从相机拍照并将该图像上传到网络服务器。这个代码工作正常。现在在其他屏幕上我从网络服务器下载图像如果从图库中选择图像,图像正在图像视图中显示,但是如果从图像视图中捕获图像,则图像未在图像视图中显示,即使文件也存在问题存在于SD卡中
I am created a app in which i am allowing user to select image from gallery or take a picture from camera and upload that image to web server.This code works fine.Now in other screen i am downloading the image from web server and storing that in sd card.The problem that if image is selected from the gallery the image is being displayed in the image view,but if the image is captured from the camera that image is not being displayed in the image view even though the file is present in the sd card
显示图像并从服务器下载的代码
Code to display image and download from server
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;
}
推荐答案
您应该在显示前缩放图像它到ImageView。
一旦我遇到同样的问题,缩放位图就解决了我的问题。
You should scale image before display it to ImageView.Once i was facing same problem and scaling of bitmap solve my problem.
以下代码是这样做的 -
Below is code to do so-
Bitmap b = BitmapFactory.decodeByteArray(bitmapProfilePic , 0, bitmapProfilePic .length)
ivProfilePic.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
希望这能解决您的问题
一切顺利。
这篇关于位图图像未显示在imageview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!