Reputation: 3
I have a problem. When the app has loaded an image from a web it should show up but on Android 4.0 it doesn't show up. It works on android 2.2.
Here is the code.
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imgView =(ImageView)findViewById(R.id.imview);
Drawable drawable = LoadImageFromWeb("http://alphaapplication.com/liveshit/free/tour_dates/images/ad.png");
imgView.setImageDrawable(drawable);
}
public void ad (View view) {
ad("https://market.android.com/search?q=pub%3A%22Alpha%20Application%22");
}
private void ad (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
private Drawable LoadImageFromWeb(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
Upvotes: 0
Views: 923
Reputation: 7349
pr0methium is right. http://developer.aiwgame.com/imageview-show-image-from-url-on-android-4-0.html
....... // show The Image new DownloadImageTask((ImageView) findViewById(R.id.imageView1)) .execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"); }
public void onClick(View v) {
startActivity(new Intent(this, IndexActivity.class));
finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
Upvotes: 1
Reputation: 2481
Starting in Honeycomb, 3.X.X, you can no longer access the network from your main UI thread. Which is what you're doing in the code snippet you just provided. Now, you need to move all network accesses to a background thread. The easiest way to accomplish this is by using AsyncTask. I've linked you to the developer guide that should make it pretty easy for you to transition your code.
http://developer.android.com/resources/articles/painless-threading.html
Just to validate, you should look in logcat, and your app would probably have quietly thrown a NetworkOnMainThreadException, but it's not enough to cause your app to blow up because it's informational; not a system halt.
Let me know if that works for you, or else look for the exception being thrown in logcat and reply here
DSC
Upvotes: 2