hharry_tech
hharry_tech

Reputation: 992

Images not showing properly in listview in android

I am downloading multiple attachments then writing images in sdcard but my problem is that when display image from sdcard on list showing half black image I am using picasso image loader for show images from sdcard. I getting same problem with glide images loader. see screenshot enter image description here

Note: Image is getting downloaded properly when I see it in sdcard.

//showing image from sdcard

Picasso.get().load(uri).resize(200, 200).centerCrop().into(holder.imageAttach);

//Code Download Image by URL and write in sdcard

URL url = new URL(urlString);//Create Download URl
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();//Open Url Connection
urlConnection.setRequestMethod("GET");//Set Request Method to "GET" since we are getting data
urlConnection.connect();//connect the URL Connection
double fileSize = 0;

//Create New File if not present
if (!filePath.exists()) {
    filePath.createNewFile();
}

//If Connection response is not OK then show Logs
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
    Log.e("AutoDownloader", "Server returned HTTP "
        + urlConnection.getResponseCode()
        + " " + urlConnection.getResponseMessage());
}

BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filePath));//Get OutputStream for NewFile Location

InputStream is = urlConnection.getInputStream();//Get InputStream for connection

byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
while ((len1 = is.read(buffer)) != -1) {
    fos.write(buffer, 0, len1);//Write new file
}

//Close all connection after doing task
fos.close();
is.close();

Log.i("AutoDownloader", "downloadAttachmentInByte startDownload success");
try{
    ((Activity)ContextHelper.getContext()).runOnUiThread(new Runnable() {
        @Override
                public void run() {
                        progressBar.setVisibility(View.GONE);
                }
         });
} catch (Exception e){
     e.printStackTrace();
}

Upvotes: 0

Views: 301

Answers (3)

Dnyaneshwar Panchal
Dnyaneshwar Panchal

Reputation: 444

You just remove resize(200,200) and use only fit().

Picasso.with(this).load(uri).fit().into(holder.imageAttach);

Upvotes: 0

Gowthaman M
Gowthaman M

Reputation: 8282

try adding android:scaleType="fitXY" to your ImageView.

Picasso.get().load(uri).resize(200, 200).centerCrop().into(holder.imageAttach);

replace simply use this

  holder.imageAttach.setImageURI(uri)

Upvotes: 0

karanatwal.github.io
karanatwal.github.io

Reputation: 3673

I think your images are not getting downloaded properly. I would suggest to Let the Picasso do the whole work for you. -

Target target = new Target() {  
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        holder.imageAttach.setImageBitmap(bitmap);
          new Thread(new Runnable() {

                    @Override
                    public void run() {

                        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
                        try {
                            file.createNewFile();
                            FileOutputStream ostream = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                            ostream.flush();
                            ostream.close();
                        } catch (IOException e) {
                            Log.e("IOException", e.getLocalizedMessage());
                        }
                    }
                }).start();
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};

  Picasso.with(getApplicationContext())
      .load(photoUrl)
      .resize(200, 200)
      .into(target);

Upvotes: 1

Related Questions