简体   繁体   中英

Unable to download image from the url

Hi please help

Bitmap is null when running on a device but it runs fine when running on Genymotion(Emulator)

I'm trying to download image from a URL when i'm running it in Genymotion(Emulator) its running fine and displaying the image,but when i'm running it on a device Bitmap is null.

Below is the code

public void DisplayImage(String url, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap = memoryCache.get(url);
    if (bitmap != null)
        imageView.setImageBitmap(bitmap);
    else {
        queuePhoto(url, imageView);
        imageView.setImageResource(stub_id);
    }
}

and this is the url

url=http://www.hugosys.in/www.nett-torg.no/wp-content/uploads/2014/04/Lighthouse3-300x225.jpg

Please help me.

You should use jsoup.jar file for downloading image from server.

For Ex.

    String baseUrl = "URL Here";
    try 
    {
        Document doc = Jsoup.connect(baseUrl).get();
        Elements content = doc.select("a");
        Element last = content.last();
        lastEle=last.text();
        String temp="";
        int totalSize;
        //
        while (!a)
        {
            temp = content.get(i).text();
            a=lastEle.equals(temp);
            File f1 = new File(path);

            File f = new File(f1, temp);
            //Log.d("Image path", f+"");    
            tmpUrl = baseUrl + temp;
        //  Log.d("Full URL", tmpUrl);
            if(!f.exists())
            {
                url = new URL(tmpUrl);
                HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();

                FileOutputStream fileOutput=new FileOutputStream(f);

                InputStream inputStream = urlConnection.getInputStream();
                totalSize = urlConnection.getContentLength();

                int downloadedSize = 0;
                byte[] buffer = new byte[1024];
                int bufferLength = 0;
                str= "";
                while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
                {
                    fileOutput.write(buffer, 0, bufferLength);
                    downloadedSize += bufferLength;
                }
            }
            else
            {
                Log.d("Image Available", "Available");
            }
            i++;
        }    
    } 
    catch (IOException e) 
    {
        Log.e(TAG, "Failed to load HTML code", e);
    }

You can try loading the bitmap like this:

              public  Bitmap getBitmapFromUrl(String url) {
                    Bitmap bitmap = null;
                    HttpGet httpRequest = null;
                    httpRequest = new HttpGet(url);
                    HttpClient httpclient = new DefaultHttpClient();

                    HttpResponse response = null;
                    try {
                        response = (HttpResponse) httpclient.execute(httpRequest);
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    if (response != null) {
                        HttpEntity entity = response.getEntity();
                        BufferedHttpEntity bufHttpEntity = null;
                        try {
                            bufHttpEntity = new BufferedHttpEntity(entity);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        InputStream instream = null;
                        try {
                            instream = bufHttpEntity.getContent();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        bitmap = BitmapFactory.decodeStream(instream);

                    }
                    return bitmap;
              }

Here is the code segments which really helps you..

The Files contains Download and set image on image view using ImageLoader.Java and their is ImageCache maintained for saving time..

FileCache.java

ImageLoader.java

MemoryCache.java

Utils.java

Add Permissions in Manifest.xml

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

And code to set image on image view is...

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {


    private Button button;
    private ImageView image;
    ImageLoader imageLoader;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final String url="http://www.hugosys.in/www.nett-torg.no/wp-content/uploads/2014/04/Lighthouse3-300x225.jpg";
        imageLoader=new ImageLoader(MainActivity.this);
        button=(Button)findViewById(R.id.button1);
        image=(ImageView)findViewById(R.id.imageView1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                imageLoader.DisplayImage(url, image);
            }
        });
    }

 }

After checking ur code, i come to know that there is lack of permissions in the manifest thats why it is creating the problem

Case 1--> When u use ur code in genymotion it works fine since there is no need of read write access in your simulator.

Case 2--> When u run in device it needs the proper permisions to store the images in device cache that's y it is not working in device.

So you have to add two more permissions in the manifest..

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

Hope this works, feel free to ask me back...!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM