下载图像并在ImageView中查看时,“decoder-> decode返回false”

时间:2012-02-16 05:27:53

标签: 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;
     }

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

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

我总是"decoder->decode returned false"

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

1 个答案:

答案 0 :(得分:1)

这是形象问题。