bitmap image not displayed in imageview

2019-06-24 14:21发布

问题:

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;
    }

回答1:

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

hope this will solve your problem

All the best.



回答2:

Got this issue on a KITKAT(API 19) device, but not on device running on LOLLIPOP_MR1(API 22). I guess with API 22 or above no need to do the createScaledBitmap, but for device running on API 19 or below, it will need something like this:

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