2013-12-21 71 views
0

我能够在Imgur上传图片,并使用API​​网站中给出的示例获取链接。 我不能做的是从URL中检索位图或Drawable。 我找不到关于它的文档。显示来自imgur的图片

URL url= "http://imgur.com/1awAsRh" 
Bitmap mPic = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 

mPic为空。

我看到网站上的图片网址http://i.imgur.com/1awAsRh.jpg 我看到的图片可以PNG JPG等...

有没有办法直接获取图片的URL,是它总是相同的模式?

Tx提前!

回答

0

尝试使用此,它实现了的AsyncTask做的东西在后台(需要Android 4.0以上版本):

class loadimage extends AsyncTask<Void, Void, Void> { 
    ProgressDialog pdLoading = new ProgressDialog(YourClass.this); 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pdLoading.setMessage("Loading Image..."); 
     pdLoading.show(); 
    } 

    @Override 
    protected void doInBackground(Void... params) { 
     String stringurl = "http://imgur.com/1awAsRh" 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(stringurl).getContent()); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     pdLoading.dismiss(); 
     // do what you want with your bitmap 
     return null; 
    } 
} 

然后在这样调用你的onCreate

new loadimage().execute(); 

您还需要确保您的清单中包含以下内容:

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

正是像我一样,但线 位图位= BitmapFactory.decodeStream( (InputStream)new URL(stringurl).getContent()); 返回null。应该是因为img是http://i.imgur.com/1awAsRh.jpg而不是http://imgur.com/1awAsRh –