繁体   English   中英

无法从网址下载图片

[英]Unable to download image from the url

嗨,请帮忙

在设备上运行时,位图为null,但在Genymotion(Emulator)上运行时,它运行正常

当我在Genymotion(Emulator)中运行图像并显示图像时,我试图从URL下载图像,但是当我在设备上运行图像时,位图为null。

下面是代码

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);
    }
}

这是网址

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

请帮我。

您应该使用jsoup.jar文件从服务器下载图像。

对于前

    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);
    }

您可以尝试像这样加载位图:

              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;
              }

这是真正可以帮助您的代码段。

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

FileCache.java

ImageLoader.java

MemoryCache.java

实用工具

在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"/>

在图像视图上设置图像的代码是...

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);
            }
        });
    }

 }

在检查了您的代码后,我知道清单中缺少权限,这就是为什么它会造成问题

情况1-> 当您在genymotion中使用您的代码时,由于不需要在模拟器中进行读写访问,因此可以正常工作。

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

因此,您必须在清单中添加另外两个权限。

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

希望它能奏效,请随时问我... !!!!

暂无
暂无

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

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