2015-02-07 101 views
1

我無法從互聯網下載圖片。從網絡上傳圖片

public class ImageUploadActivity extends ActionBarActivity { 

    private final String myURL = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"; 
    private ImageView uploadImage; 
    Bitmap bitmap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_image_upload); 

     uploadImage = (ImageView) findViewById(R.id.upload_image); 
     bitmap = getBitmapFromURL(myURL); 
     uploadImage.setImageBitmap(bitmap); 

    } 

    private Bitmap getBitmapFromURL(String src) { 
     try { 
      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (Exception e) { 
      e.getStackTrace(); 
      return null; 
     } 
    } 
} 

有沒有例外,但我的ImageView仍然是空的。我也試圖用AsyncTask來做到這一點,但結果是一樣的。

02-07 05:04:18.661 30323-30346/com.myapplication4.app D/OpenGLRenderer﹕ Render dirty regions requested: true 
02-07 05:04:18.674 30323-30323/com.myapplication4.app D/Atlas﹕ Validating map... 
02-07 05:04:18.808 30323-30346/com.myapplication4.app I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328 
02-07 05:04:18.811 30323-30346/com.myapplication4.app I/OpenGLRenderer﹕ Initialized EGL, version 1.4 
02-07 05:04:18.841 30323-30346/com.myapplication4.app D/OpenGLRenderer﹕ Enabling debug mode 0 

我該如何解決這個問題?

+0

也叫'connection.disconnect();'從流 – 2015-02-07 02:14:35

回答

0

2周這邊的事情:

使用https://在您的網址如下:

private final String myURL ="https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"; 

而且因爲你在App的主線程,這始終是嚴格的NO-NO做IO,你的代碼是導致IOException。

如果你調試代碼,你會發現,有IOException的下面一行 connection.connect();

爲了解決這個問題,你應該做的IO操作使用AsyncTask

下面是完整的代碼使用指定的URL下載圖像:

public class MainActivity extends Activity { 

    private final String myURL ="https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"; 
    private ImageView uploadImage; 
    Bitmap bitmap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.e("MainActivity", "onCreate"); 
     setContentView(R.layout.activity_main); 
     uploadImage = (ImageView) findViewById(R.id.imageView1);   
     new MyDownloadTask().execute();   
    } 


    class MyDownloadTask extends AsyncTask<Void,Void,Void> 
    { 
     protected void onPreExecute() { 
      //display progress dialog. 

    } 
     protected Void doInBackground(Void... params) {   
      try { 
       URL url = new URL(myURL); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoInput(true); 
       connection.connect(); 
       InputStream input = connection.getInputStream(); 
       Bitmap myBitmap = BitmapFactory.decodeStream(input); 
       uploadImage.setImageBitmap(myBitmap); 
       connection.disconnect(); 

      } catch (Exception e) { 
       e.getStackTrace(); 
       return null; 
      } 
      return null;   
    } 
    protected void onPostExecute(Void result) { 
     Log.e("MainActivity", "onPostExecute");   
    } 
    } 
} 

,下面將在完成我的示例應用程序中的UI顯示下載後的圖像。

enter image description here

+0

得到位圖後,我做同樣的事情,但我想象沒有加載。也許項目的錯誤配置? – Fredisson 2015-02-07 11:42:58

+0

檢查2件事。 1.我有這個權限:<使用權限android:name =「android.permission.ACCESS_NETWORK_STATE」/>'2.如果你沒有看到圖像加載,只需按HOME鍵,然後再次打開你的應用程序,因爲我也發現應用在asynctask完成執行後沒有自動刷新 – AADProgramming 2015-02-07 11:47:13

+0

仍然無效。 logcat中也沒有例外。但是,您如何設法與doInBackground方法中的GUI元素進行交互?在你的NotificationBar下載圖像時會發生什麼?也許我的Android SDK中缺少一些東西? – Fredisson 2015-02-07 14:00:36

0

就在這個庫(AQuery)添加到項目中。這個庫非常輕巧,非常簡化了我的生活。

如下使用它:

public class MainActivity extends Activity { 

private final String myURL ="https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"; 
private ImageView uploadImage; 
Bitmap bitmap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    AQuery aq = new AQuery(this); 
    uploadImage = (ImageView) findViewById(R.id.imageView1);   
    aq.id(uploadImage).image(myUrl, false, true); 
    //it caches the image so the second time you try to download the image it reads from cache file and it is gonna be so fast 
} 
} 
+0

這是一項測試工作,我不必使用第三方庫。 – Fredisson 2015-02-07 11:33:20

+0

對不起! did not知道:) – 2015-02-08 18:47:55

+0

但這也是一個有用的圖書館。在未來,我會知道它:) – Fredisson 2015-02-08 19:10:09