开发者

how to get url from the file into bitmap object

I am new in Android. I am downloading images from the internet in the ListView .I getting the url in the file object but when I send it into the Bitmap object the bitmap object is return null means image is not loaded into the bitmap object.please reply me. the code is here:

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.decode开发者_JAVA百科File(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) {} 
  }
}


I do not think you are downloading properly the bitmap.

CODE

This is a function I created that will take a url from you and it will return a drawable! It will save it to a file and get it if it exists

If not, it will download it and return the drawable. You can easily edit it to save file to your folder instead.

/**
     * 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()));
        }
    }


Only thing I can think of here is that you're missing INTERNET permission in your manifest.

Try adding <uses-permission android:name="android.permission.INTERNET" /> in your AndroidManifest.xml if it's not there yet

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜