1

您好,我使用加载图像来加载图像。但是我的图像没有加载。我为此工作并开发了一个演示程序,如下所示。

@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();

    }
}

这没用。但是,如果我当时更改了图像的 URL,图像就会成功加载。我的问题是这个特定的 url 没有加载到我的 android 应用程序中。请帮我找到这个。我花了一天时间。提前致谢。

-哈迪克

4

3 回答 3

2

我使用以下内容从端点获取图像

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

也许你可以适应这个


好的,我下载了示例并查看了 URL - 问题似乎是 HTTP => HTTPS 重定向(我提到我必须在示例中更改它们 - 我还注意到当我尝试加载时我的浏览器被迫切换到 https您提供的字符串中的图像)。该示例似乎无法正确处理该问题,但是如果我将 ImageLoader.getBitmap 更改为此,则可以

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;
    }
}
于 2012-08-20T05:16:33.420 回答
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...
于 2012-08-17T09:46:21.400 回答
1

花了2天后,我终于来这里回答我自己的问题。主要问题是文件名。我更改了文件的文件名和文件位置,我的问题得到了解决。谢谢所有给我想法的人。

还有一件事我知道我用 https:// 修改了我的 http://

于 2012-08-20T09:30:08.777 回答