【问题标题】:bitmap image not displayed in imageview位图图像未显示在图像视图中
【发布时间】: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


【解决方案1】:

您应该先缩放图像,然后再将其显示到 ImageView。 一旦我面临同样的问题,位图的缩放解决了我的问题。

以下是执行此操作的代码-

Bitmap b = BitmapFactory.decodeByteArray(bitmapProfilePic , 0, bitmapProfilePic .length)
ivProfilePic.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

希望这能解决您的问题

一切顺利。

【讨论】:

  • 这是一个正确的答案。 ImageViews 中的大图像曾经存在问题。
  • 我没有得到 bitmapProfilePic .length
  • 我没有得到长度
【解决方案2】:

在 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多