0

Hi guys surfing the internet I found some snippet to download and set an Image on a ImageView on Android. But it seems that doesn't works because it don't find the resource... Here is my snippet

String user_image_url = "http://www.nuoto.it/foto_news/papera";
URL url;
try {
    url = new URL(user_image_url);
    HttpURLConnection conn;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true); 
        conn.connect();
        InputStream is;
        is = conn.getInputStream();
        Bitmap bmImg = BitmapFactory.decodeStream(is);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}   


String uri = "@drawable-hdpi/bg_main.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName()); //Here it give me the error (Resource not found)

ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
Drawable res = getResources().getDrawable(imageResource);
imageview.setImageDrawable(res);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Thank you so much

Logcat for @Hunkeone:

10-13 16:55:57.926: E/AndroidRuntime(4380): FATAL EXCEPTION: main
10-13 16:55:57.926: E/AndroidRuntime(4380): Process: com.example.findmyclients, PID: 4380
10-13 16:55:57.926: E/AndroidRuntime(4380): java.lang.NullPointerException
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.content.ContextWrapper.getResources(ContextWrapper.java:94)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.example.findmyclients.BuildInfoMatrix.Read_Matrix(BuildInfoMatrix.java:172)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.example.findmyclients.WindowFeature.show(WindowFeature.java:51)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.example.findmyclients.MainActivity$7.onInfoWindowClick(MainActivity.java:679)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.android.gms.maps.GoogleMap$9.e(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.android.gms.maps.internal.f$a.onTransact(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.os.Binder.transact(Binder.java:361)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.android.gms.maps.internal.ai.a(SourceFile:82)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.c.ae.h(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.c.g.b(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.bo.aK_(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.bo.a(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.ca.c(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.am.onSingleTapConfirmed(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.i.g.onSingleTapConfirmed(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.i.i.handleMessage(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.os.Looper.loop(Looper.java:146)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.app.ActivityThread.main(ActivityThread.java:5602)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at dalvik.system.NativeStart.main(Native Method)
Pierpaolo Ercoli
  • 1,028
  • 2
  • 11
  • 32

3 Answers3

2

Use Picasso from square.. Its awesome

Using this you can load images in imageview with single line of code. e.g.

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
  • It has inbuilt support of Disk & memory cache(Lrucache), which is required for faster image retrievals in case of Listview/Grid views.
  • It downloads images from server asynchronously & set image in ImageView on completion of image download.
Akhil
  • 6,667
  • 4
  • 31
  • 61
1

The path you are giving to your String uri should just be @drawable/bg_main but there are easy ways to assign a image to an ImageView. Instead of doing:

String uri = "@drawable-hdpi/bg_main.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName()); 
ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
Drawable res = getResources().getDrawable(imageResource);
imageview.setImageDrawable(res);

You can do

ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
imageview.setImageDrawable(R.drawable.bg_main);

And that's it!

Carlos J
  • 2,965
  • 4
  • 17
  • 28
1

After you've received a bitmap image use drawable bitmap to set it to the layout.

ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
imageview.setImageBitmap(bitmap);
imageview.setAdjustViewBounds(true);
Drawable drawable= new BitmapDrawable(getResources(),bitmap);
imageview.setImageDrawable(drawable);

I would also recommend to make default image in case image has not been uploaded (no internet connection etc.

Hunkeone
  • 505
  • 5
  • 11
  • I will try and I will comment the result :) – Pierpaolo Ercoli Oct 13 '14 at 14:52
  • Doesn't work @Hunkeone :/ I post in a edit the logcat – Pierpaolo Ercoli Oct 13 '14 at 14:56
  • That means your bitmap is empty Bitmap bmImg = BitmapFactory.decodeStream(is); make smthing like this check: ImageView imageview = (ImageView)v.findViewById(R.id.place_image); if(bmImg!=null){ imageview.setImageBitmap(bitmap); imageview.setAdjustViewBounds(true); Drawable drawable= new BitmapDrawable(getResources(),bitmap); imageview.setImageDrawable(drawable); }else{ imageview.setImageDrawable(R.Layout.yourPicture) } – Hunkeone Oct 14 '14 at 06:02