android - 下载图片并在ImageView中查看时为"decoder->decode returned false"

标签 android imageview

我尝试使用 FlushedInputStream :Android decoder->decode returned false for Bitmap download

但没有任何变化,因为我使用:BitmapFactory.decodeFile(path_of_my_downloaded_file), 不使用 BitmapFactory.decodeStream

这是我的下载文件代码:

  public static boolean downloadFile(String url, String dir, String name){
         Log.i("Start Downloading ", "=");
       //    Create download folder:
         File f = new File(dir);
         if(!f.exists()){
             f.mkdirs();
         }
        try {
            File fTo = new File(dir, name);
            URL downloadUrl = new URL(url);
            //create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) downloadUrl.openConnection();
            //set up some things on the connection
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            //and connect!
            urlConnection.connect();
            FlushedInputStream in = new FlushedInputStream(downloadUrl.openStream());

//          in = new FlushedInputStream(in);

            byte[] buffer= new byte[4096];
//          Write file to toFolder
            FileOutputStream os = new FileOutputStream(fTo);
            try {
                 do{
                     int numread = in.read(buffer);  
                     if (numread <= 0)  {
                         break;
                     } 
                     os.write(buffer, 0, numread);
                }while(true);
            } catch (ConnectTimeoutException e) {
                e.printStackTrace();
                return false;

            }
            if (os != null) {
                os.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            Log.e("Error reading file", e.toString());
            return false;
        }
        return true;
     }

这是我将位图设置为 ImageView 的代码:

Bitmap bitmap = BitmapFactory.decodeFile(my_file);
mImageView.setImageBitmap(bitmap);

我总是"decoder->decode returned false"

注意:我必须先下载这张图片。

最佳答案

这是图像的问题。

关于android - 下载图片并在ImageView中查看时为"decoder->decode returned false",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9306034/

相关文章:

java - 每个敌人的生命值

android - 自定义对话框中的 ImageView - fill_parent 不起作用,我的对话框很小

Android imageView 的海拔被 TextView 遮挡

javascript - 如何在 android phonegap 中将 Canvas 图像保存到设备的 sdcard 中?

android - ViewPager PagerAdapter 删除项目

android - 如何访问android库项目中的资源

android - 如何在 Android 中显示 blob 图像?

iphone - 将网站缩放到可见区域的 100% 宽度/高度

ios - 添加滤镜时,照片被拉伸(stretch)

java - 无法在Android上使用imageview显示图像