Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从文件中获取url到位图对象_Android_Image - Fatal编程技术网

Android 如何从文件中获取url到位图对象

Android 如何从文件中获取url到位图对象,android,image,Android,Image,我是Android新手。 我正在ListView中从internet下载图像。我在文件对象中获取url,但当我将其发送到位图对象时,位图对象返回null表示图像未加载到位图对象。请回复我。代码如下: private Bitmap getBitmap(String url) { String filename = String.valueOf(url.hashCode()); File f = new File(cacheDir, filename); // here in f i ge

我是Android新手。 我正在ListView中从internet下载图像。我在文件对象中获取url,但当我将其发送到位图对象时,位图对象返回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) {} 
  }
}

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

代码

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

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


我能想到的唯一一件事是你的清单中没有互联网许可


如果AndroidManifest.xml中还没有,请尝试在其中添加

是否下载html页面作为png图像?或者URL已经指向png了吗?
/**
     * 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()));
        }
    }