0

I had develop a mobile apps to load image from URL path.

Here is my code.

    private Drawable LoadImageFromWebOperations(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;
        }
    }

This method is work well in emulator which android API 10 but cannot work in android device. The device i use test is API 16 and 17.

Kindly look for help. Thanks.

XZheng
  • 1
  • 6

3 Answers3

0

Have you done this?:

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

and yes, you must be connected to the internet.

LotusUNSW
  • 2,027
  • 18
  • 20
  • I had use the INTERNET and WRITE_EXTERNAL_STORAGE in manifext. It not worked. – XZheng Nov 19 '13 at 08:34
  • Is the path correct? go here for more info: http://stackoverflow.com/questions/13225456/android-application-works-fine-in-emulator-but-not-in-real-device – LotusUNSW Nov 19 '13 at 08:53
0

LotusUNSW is right, you have to add INTERNET permission but if it's working on emulator it seems that this permission is set .

EDIT : Alternatively, you can try using this method :

(last code was a stupid copy paste without checking, available here)

Don't forget to do this in a separate thread (using AsyncTask for example)

But try this thing instead

private Bitmap getImageBitmap(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
       } catch (IOException e) {
           Log.e(TAG, "Error getting bitmap", e);
       }
       return bm;
    } 

and then

Bitmap b = getImageBitmap("http://sdsdsdsdsdsdsds");
if (b != null)
  myImageView.setImageDrawable(d);

Source :here

Community
  • 1
  • 1
azerto00
  • 1,001
  • 6
  • 18
0

Try to move this code to a thread or AsyncTask. Http requests can't be done from main thread because of strict mode.Here check AsyncTask.

Ercan
  • 3,705
  • 1
  • 22
  • 37