从URL检索位图

时间:2013-10-06 05:32:37

标签: android bitmap

虽然不是很优雅,但我使用快速黑客来解决这个问题。但是,我仍然希望发现错误。

关于flickr API,

我有一个想要Bitmap对象的图片,我可以访问用户ID grimneko和图片ID 8638235984

因此,我试过

URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/");
Bitmap mp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

但是,在这种情况下,mp为null。我还尝试将www.google.com作为url,但似乎也无效。

但是,http://3.bp.blogspot.com/-sUAEV-EIlyY/T4WIGDkoLpI/AAAAAAAACKI/epNLfw01cW0/s320/corgi+working.jpg确实有效。

我的猜测是Bitmap个对象只能从具有图片扩展名的网址创建。它是否正确?

如果是这种情况,我如何在图像视图中从该flickr网址渲染图像?

2 个答案:

答案 0 :(得分:0)

试试这个..

   public static Bitmap getBitmapFromURL() {
        try {
            URL url = new URL("http://www.flickr.com/photos/grimneko/8637130199/lightbox/");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
}

并参考此链接..

How to load an ImageView by URL in Android?

答案 1 :(得分:0)

private class setProfileData extends AsyncTask<Void, Void, Bitmap> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading ..");
        progressDialog.setCancelable(true);
        progressDialog.show();

    }

    @Override
    protected Bitmap doInBackground(Void... arg0) {
        try {

            String imgurl = imageURL.replace(" ", "%20");
            Log.e("Profile pic path ----------->>>>>", imgurl);
            ProfilePicBitmap = null;
            // URL url = new URL(imgurl);
            try {
                Bitmap ProfilePicBitmap = null;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                try {
                    ProfilePicBitmap = BitmapFactory.decodeStream(
                            (InputStream) new URL(imgurl).getContent(), null, options);

                } catch (Exception e) {

                    e.printStackTrace();
                    try {
                        ProfilePicBitmap = BitmapFactory.decodeStream((InputStream) new URL(imgurl)
                        .getContent());

                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                ProfilePicBitmap = BitmapFactory.decodeResource(
                        getResources(), R.drawable.ic_launcher);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ProfilePicBitmap;

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

        try {
            if (ProfilePicBitmap != null) {
                ImageView.setImageBitmap(ProfilePicBitmap);
            } else {
                ImageView.setImageBitmap(BitmapFactory
                        .decodeResource(getResources(),
                                R.drawable.ic_launcher));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onCancelled() {

        super.onCancelled();
        progressDialog.dismiss();

    }
}