如何以编程方式将图像添加到我的Android应用程序

时间:2014-05-27 18:12:56

标签: java android image url

我希望能够将url用作图像源。我找到了以下函数来转换为位图。但是,我收到了“NetworkOnMainThreadException”。使用Log我发现了

conn.connect();

,是函数崩溃的地方。

我还发现因为异常我应该使用AsyncTask类扩展。所以我在下面的代码的第二部分中使用它。我仍然得到同样的例外。我并不完全理解错误意味着什么,我试图在同一个地方同时做太多事情。

是否有更简单的方法将网址转换为我可以使用的图片?我该如何修复现有的功能?

 public static Bitmap getImageBitmap(String url) {
            Log.i("bitmap","here?");
            Bitmap bm = null;
            Log.i("bitmap","here?");
            try {
                Log.i("bitmap","here?");
                URL aURL = new URL(url);
                Log.i("bitmap","here?");
                URLConnection conn = aURL.openConnection();
                Log.i("bitmap","last ?");
                conn.connect();
                Log.i("bitmap","here?");
                InputStream is = conn.getInputStream();
                Log.i("bitmap","here?");
                BufferedInputStream bis = new BufferedInputStream(is);
                Log.i("bitmap","here?");
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                Log.i("bitmap","here?");
                is.close();
                Log.i("bitmap","here?");
           } catch (IOException e) {
               Log.e("img", "Error getting bitmap", e);
           }
           return bm;
        }   

使用AsyncTask:

class taskRunner extends AsyncTask<String, URL, Bitmap>{

     public static Bitmap getImageBitmap(String url) {
            Log.i("bitmap","here?");
            Bitmap bm = null;
            Log.i("bitmap","here?");
            try {
                Log.i("bitmap","here?");
                URL aURL = new URL(url);
                Log.i("bitmap","here?");
                URLConnection conn = aURL.openConnection();
                Log.i("bitmap","last ?");
                conn.connect();
                Log.i("bitmap","here?");
                InputStream is = conn.getInputStream();
                Log.i("bitmap","here?");
                BufferedInputStream bis = new BufferedInputStream(is);
                Log.i("bitmap","here?");
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                Log.i("bitmap","here?");
                is.close();
                Log.i("bitmap","here?");
           } catch (IOException e) {
               Log.e("img", "Error getting bitmap", e);
           }
           return bm;
        }   

非常感谢您花时间阅读本文。

1 个答案:

答案 0 :(得分:0)

首先,不要忘记清单中的互联网许可

<uses-permission android:name="android.permission.INTERNET"/>

Seconddthing是你的实现错误..

class BitmapTaks extends AsyncTask<ImageView, Void, Bitmap> {

protected Bitmap doInBackground(ImageView... imageview) {
   Log.i("bitmap","here?");
        Bitmap bm = null;
        Log.i("bitmap","here?");
        try {
            Log.i("bitmap","here?");
            URL aURL = new URL(urls[0]);
            Log.i("bitmap","here?");
            URLConnection conn = aURL.openConnection();
            Log.i("bitmap","last ?");
            conn.connect();
            Log.i("bitmap","here?");
            InputStream is = conn.getInputStream();
            Log.i("bitmap","here?");
            BufferedInputStream bis = new BufferedInputStream(is);
            Log.i("bitmap","here?");
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            Log.i("bitmap","here?");
            is.close();
            Log.i("bitmap","here?");
       } catch (IOException e) {
           Log.e("img", "Error getting bitmap", e);
       }
       return bm;
}

protected void onPostExecute(Bitmap b) {
   //do something after execute
}
}

调用它的方法是:

new BitmapTaks().execute(url);

应该是这样的,希望有所帮助

编辑:

AsyncTask - 这是异步结构..

在这种情况下,param是ImageView,您还需要为URL传递String。 结果将是位图,将在OnPostExecute方法上设置。

我写下了上面的例子以便了解..您可以查看下一个链接:http://chrisharrington1.wordpress.com/2012/01/24/android-setting-a-downloaded-image-to-an-imageview/

如果您还有其他需要,请告诉我