简体   繁体   English

无法从网址下载图片

[英]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) 在设备上运行时,位图为null,但在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. 当我在Genymotion(Emulator)中运行图像并显示图像时,我试图从URL下载图像,但是当我在设备上运行图像时,位图为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. 您应该使用jsoup.jar文件从服务器下载图像。

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.. 这些文件包含使用ImageLoader.Java在图像视图上下载和设置图像,并且为节省时间而对它们进行ImageCache维护。

FileCache.java FileCache.java

ImageLoader.java ImageLoader.java

MemoryCache.java MemoryCache.java

Utils.java 实用工具

Add Permissions in Manifest.xml 在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. 情况1-> 当您在genymotion中使用您的代码时,由于不需要在模拟器中进行读写访问,因此可以正常工作。

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. 情况2-> 当您在设备中运行时,它需要适当的权限才能将图像存储在设备缓存中,这在设备中不起作用。

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...!!!! 希望它能奏效,请随时问我... !!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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