位图图像不在图像视图中显示
问题描述:
我创建了一个应用程序,我允许用户从图库中选择图像或从相机拍摄图片并将该图像上传到网络server.This代码工作正常。现在在其他屏幕我从网络服务器下载图像并将其存储在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;
}
答
在将图像显示到ImageView之前,应该缩放图像。 一旦我面临同样的问题,缩放位图解决了我的问题。
下面的代码做所谓
Bitmap b = BitmapFactory.decodeByteArray(bitmapProfilePic , 0, bitmapProfilePic .length)
ivProfilePic.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
希望这将解决您的问题
所有最优秀的。
答
在KITKAT(API 19)设备上获得此问题,但在LOLLIPOP_MR1(API 22)上运行的设备上没有此问题。我想用API 22以上无需做createScaledBitmap,但在API 19或低于运行设备,就需要这样的:
Bitmap myPictureBitmap = BitmapFactory.decodeFile(imagePath);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
myPictureBitmap = Bitmap.createScaledBitmap(myPictureBitmap, ivMyPicture.getWidth(),ivMyPicture.getHeight(),true);
}
ivMyPicture.setImageBitmap(myPictureBitmap);
我能看到的代码从拍照相机并将其显示到图像视图? – Shrey 2015-02-23 12:11:07
程序崩溃吗? – mes 2015-02-23 12:29:08