2014-04-24 18 views
0

我正在爲學生開發一款應用程序,並在真實設備上測試時發現錯誤。這是我的應用程序的一段代碼。我使用這個AsynTask通過網頁檢索信息,工作得很好。爲什麼我無法使用真實設備在Android應用上加載互聯網圖像?

protected void onPostExecute(PlaceRecord result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 

     // Refresh the values 
     ImageView imageFlag = (ImageView) findViewById(R.id.imageViewFlag); 

     try { 
      InputStream is = (InputStream) new URL(place.getFlagUrl()) 
        .getContent(); 
      Drawable d = Drawable.createFromStream(is, "src name"); 
      Log.i(TAG, "D: " + d.toString()); 
      if (d.toString() == "") { 
       imageFlag.setImageResource(R.drawable.logo_fondo); 
      } else { 
       imageFlag.setImageDrawable(d); 
      } 
     } catch (Exception e) { 
      Log.e(TAG, "que conho pasa: " + e); 
      e.printStackTrace(); 
     } TextView textVCountry = (TextView) findViewById(R.id.textViewCountryName); 
     textVCountry.setText(place.getCountryName()); 
     TextView textVPlace = (TextView) findViewById(R.id.textViewResolverPlace);  
     textVPlace.setText(place.getPlace());   

這是事情,當我在我的模擬器上運行這是完美的作品。結果是具有String countryName,String place和一個FlagURL的對象,它是Web上圖像的URL。當我在模擬器上運行它時,我得到的圖像和兩個文本都很完美,但是當我在真實設備上運行它時(起初我使用了一個位圖,但它只是崩潰了,我不知道爲什麼......而logcat窗口爲真正的設備沒有giv e我很多幫助),然後用這種方法它不會崩潰,但是當我運行它時,它不顯示任何東西..在它顯示的模擬器上,如果你更改另一個圖像的url,它會顯示新的一個,但在真正的一個不行。

我可以告訴你更多的代碼,如果你想,我只是不想填充代碼。問題是,在logcat的窗口犯規說明了什麼?

一個重要的事情是,我的真實設備具有Android 4.2版本,但模擬器2.3

任何客人?

謝謝

這是我的清單文件

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="advance.modelling.yourvistit" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="19" /> 

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="advance.modelling.yourvisit.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

     </application> 


    </manifest> 
+0

登錄貓和Android清單的細節將有所幫助 –

回答

0

您必須在後臺線程與互聯網合作。將下載圖像的代碼移動到您的AsyncTask類的doInBackground()函數。在onPostExecute你應該在你ImageView集已經下載圖像

獲取在主線程的圖像只能換舊的Android設備正常工作(因爲你可以在享有隻能在主線程工作)。

+0

非常感謝你! :) – neteot

相關問題