android - 图片在 android 的 listview 中显示不正确

标签 android picasso android-sdcard imagedownload

我正在下载多个附件,然后在 sdcard 中写入图像,但我的问题是,当在显示半黑图像的列表中显示来自 sdcard 的图像时,我正在使用 picasso 图像加载器来显示来自 sdcard 的图像。我在使用 glide images loader 时遇到了同样的问题。看截图enter image description here

注意:当我在 SD 卡中看到图片时,它正在正确下载。

//显示sdcard中的图片

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

//代码通过URL下载图片并写入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();
}

最佳答案

我认为您的图像没有正确下载。我建议让 Picasso 为您完成所有工作。 -

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

关于android - 图片在 android 的 listview 中显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50056288/

相关文章:

android - 当我单击android中的You-tube链接时如何打开自定义视频播放器?

java - 无法在 Android 中使用 Picasso 从 URL 加载图像

android - Aquery vs ButterKnife 和 picasso

Android 模拟器 SD 卡镜像已在使用中

android - 无法从 Windows CMD 行检索 SHA-1 key

android - 创建APK扩展文件的步骤

java - 如何在开始和测试期间使用 Appium "wait to activity"?

android - 将 Picasso 与 Image Getter 结合使用

android - 从服务器下载时,listview 将图像存储在 android 中的位置

javascript - 如何使用 loadDataWithBaseURL() 方法从 SDCARD 加载 JS 和 CSS