如何将文件中的url转换为位图对象

时间:2011-09-02 12:26:04

标签: android image

我是Android新手。 我在ListView中从互联网上下载图像。我在文件对象中获取url但是当我将其发送到Bitmap对象时,位图对象返回null表示图像未加载到位图对象中。请回复我。代码在这里:

private Bitmap getBitmap(String url) {
  String filename = String.valueOf(url.hashCode());

  File f = new File(cacheDir, filename);
// here in f i getting image url

  // here in bitmap the url is not loaded & get null
  Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
  if(bitmap != null) return bitmap;

  // Nope, have to download it
  try {
    bitmap = 
    BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
    // save bitmap to cache for later
    writeFile(bitmap, f);

    return bitmap;
  } catch (Exception ex) {
    ex.printStackTrace();
    return null;
  }
}

private void writeFile(Bitmap bmp, File f) {
  FileOutputStream out = null;

  try {
    out = new FileOutputStream(f);
    bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
  } catch (Exception e) {
    e.printStackTrace();
  }
  finally { 
    try { if (out != null ) out.close(); }
    catch(Exception ex) {} 
  }
}

2 个答案:

答案 0 :(得分:2)

我认为您没有正确下载位图。

<强> CODE

这是我创建的一个函数,它将从您那里获取一个url,它将返回一个drawable! 它会将其保存到文件中并在存在时获取

如果没有,它将下载并返回drawable。 您可以轻松编辑它以将文件保存到您的文件夹中。

/**
     * Pass in an image url to get a drawable object
     * 
     * @return a drawable object
     */
    private static Drawable getDrawableFromUrl(final String url) {
        String filename = url;
        filename = filename.replace("/", "+");
        filename = filename.replace(":", "+");
        filename = filename.replace("~", "s");
        final File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + filename);
        boolean exists = file.exists();
        if (!exists) {
            try {
                URL myFileUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) myFileUrl
                        .openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                final Bitmap result = BitmapFactory.decodeStream(is);
                is.close();
                new Thread() {
                    public void run() {
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        result.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                        try {
                            if (file.createNewFile()){
                                //
                            }
                            else{
                                //
                            }

                            FileOutputStream fo;
                            fo = new FileOutputStream(file);
                            fo.write(bytes.toByteArray());
                            fo.flush();
                            fo.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                BitmapDrawable returnResult = new BitmapDrawable(result);
                return returnResult;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        else {
            return new BitmapDrawable(BitmapFactory.decodeFile(file.toString()));
        }
    }

答案 1 :(得分:0)

我在这里只能想到的是你在清单中缺少INTERNET权限。

尝试在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" />(如果它还没有