0

虽然不是很优雅,但我使用了一个快速的技巧来解决这个问题。但是,我仍然希望找出问题所在。

关于 flickr API,

我有一个我想要一个 Bitmap 对象的图像,并且我可以访问用户 idgrimneko和图片 id 8638235984

因此,我尝试了

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

但是, mpnull在这种情况下。我也尝试过www.google.comurl而且似乎也不起作用。

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

我的猜测是Bitmap只能从具有图像扩展名的 url 创建对象。这个对吗?

如果是这种情况,我如何在图像视图中从该 flickr url 呈现图像?

4

2 回答 2

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;
        }
}

并参考此链接..

如何在 Android 中通过 URL 加载 ImageView?

于 2013-10-06T05:46:36.800 回答
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();

    }
}
于 2014-07-07T11:38:38.523 回答