Question

I am developing an app for student purposes and I found an error while testing it on a real device. Here is a piece of code of my app. I use this AsynTask to retrieve information through a webpage and that works just fine.

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());         

Here is the thing, when I run this on my emulator is works perfect. Result is an object that have String countryName, String place and a FlagURL that is a URL of a image on the web. When I run this on the emulator I get the image and the two texts all perfect, but when I run it on a real device (at first I used a bitmap but it just crashed and I didn't know why..and the logcat window for the real device didnt giv e me much help) then with this method it does not crash but when I run it it doesn't show anything.. on the emulator it shows, if you change the url for another image it shows the new one, but on the real one nope.

I can show you more code if you want, I just not wanted to fill this with code. The problem is that the logcat window doesnt show anything...

An important thing is that my real device has android 4.2 but the emulator 2.3

Any guest?

Thank you

This is my manifest file

<?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>
Was it helpful?

Solution

You must work with internet in background thread. Move code which downloads image to doInBackground() function of your AsyncTask class. In onPostExecute you should set already downloaded image in your ImageView(Because you can work with views only in main thread)

Getting image in main thread can work only for old Android devices.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top