1

Hello i use Loading Images for Loading Images. But my images are not load. I work on that and develop one Demo program that is as follows.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new DownloadImageTask((ImageView) findViewById(R.id.my_image))
            .execute("http://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Raw_Conditioner_500835f701532_175x175.jpg");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(ImageLoadExampleActivity.this);
        pd.show();
    }

    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);
        pd.dismiss();

    }
}

It does not work. But if i change URL of image at that time image is loaded successfully. My question is this perticular url is not loaded in my android application. Please help me to find this. I spend one days on this. Thanks in advance.

-Hardik

Community
  • 1
  • 1
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • What is the URL that works successfully? – mostar Aug 17 '12 at 06:48
  • http://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Natural_Shampoo_500835bcdd5b6_175x175.jpg @paritybit – Hardik Joshi Aug 17 '12 at 06:49
  • http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg @paritybit – Hardik Joshi Aug 17 '12 at 06:53
  • so it works when bottle is placed upside-down :) anyway try to give the name of the successful image to the problematic image, in order to see if the problem is related to the image file itself. – mostar Aug 17 '12 at 06:56
  • @paritybit i think problem in image i replace most of url images with other but my urls does not display images. i am stuck on this dude. – Hardik Joshi Aug 17 '12 at 06:58
  • you've said 'https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Natural_Shampoo_500835bcdd5b6_175x175.jpg' works but 'http://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Raw_Conditioner_500835f701532_175x175.jpg' does not, didn't you? – mostar Aug 17 '12 at 07:01
  • this means it is not about URL, but can be related to image file or file name. – mostar Aug 17 '12 at 07:01
  • http://pastebin.com/sZEp7ZS4 @paritybit – Hardik Joshi Aug 17 '12 at 07:04
  • maybe your android device cannot access moroccomethod.com, try browsing the images from your device's browser to see if this is the case. if so it may be related to DNS. – mostar Aug 17 '12 at 07:20

3 Answers3

2

I use the following to get images from an endpoint

URL url = new URL(imageUrl);
InputStream content = (InputStream)url.getContent();
Drawable image = Drawable.createFromStream(content , "src");    

perhaps you can adapt this


Okay I downloaded the sample and looked at URLs - the issue appears to be the HTTP => HTTPS redirection (I mentioned that I had to change them in my sample - I also notice my browser was forced to switch to https when I tried to load your images from the supplied strings). The sample does not appear to handle that correctly however if I alter ImageLoader.getBitmap to this then it does

private Bitmap getBitmap(String url) 
{
    File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from web
    try {
        Bitmap bitmap=null;

        HttpURLConnection conn;
        URL imageUrl = new URL(url);
        int rc = -1;
        do {
            conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            rc = conn.getResponseCode();
            imageUrl = conn.getURL();
        } while (rc == -1); // hmmm - perhaps a http => https

        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }
}
Shaun Wilde
  • 8,228
  • 4
  • 36
  • 56
1
public String loadImageFromWebOperations(String url, String path) {
    try {
        is = (InputStream) new URL(url).getContent();
        System.out.println(path);
        File f = new File(path);
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(f);
        try {
            byte[] b = new byte[100];
            int l = 0;
            while ((l = is.read(b)) != -1)
            fos.write(b, 0, l);
        } catch (Exception e) {
            Log.e("Error in creating file", "", e);
        }
        Log.e("created file path is :", "" + f.getAbsolutePath());
        return f.getAbsolutePath();
    } catch (Exception e) {
        System.out.println("Exc=" + e);
        return null;
    }
}

If any issue let me know...
dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Maidul
  • 1,289
  • 16
  • 30
1

After spending 2 days i finally come here to give answer on my own question. The main problem is of file names. I change file names and file location of files and my issue is solve. Thanks all which give me idea.

One also thing which i come to know that i modify my http:// with https://

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • glad you sorted it - but if you have the issue again with HTTP => HTTPS my solution should help you – Shaun Wilde Aug 22 '12 at 00:35
  • @hardikjoshi - I did I amended my answer with the solution to handle http=>https that appeared to be causing your loading issue. – Shaun Wilde Aug 22 '12 at 05:35