来自URL的ImageView

时间:2012-04-18 09:06:32

标签: android imageview

我想从网络驱动器设置ImageView位图。但是我无法连接到服务器:无法登录到服务器(PASS):141.11.11.247错误。

这是我的下载ant setBitmap代码:

public class ResimCek implements Runnable {

    int resimID = 0;

    public ResimCek(int parcaID) {
        // store parameter for later user
        resimID = parcaID;
    }

    public void run() {
        try {
            ImageView resim = (ImageView) findViewById(resimID);
            Bitmap bitmap = BitmapFactory
                    .decodeStream((InputStream) new URL(
                            "file://141.11.11.247/foto_metod/Parca/"
                                    + Integer.toString(resimID) + ".jpg")
                            .getContent());
            resim.setImageBitmap(bitmap);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

int resimID = 0; public ResimCek(int parcaID) { // store parameter for later user resimID = parcaID; } public void run() { try { ImageView resim = (ImageView) findViewById(resimID); Bitmap bitmap = BitmapFactory .decodeStream((InputStream) new URL( "file://141.11.11.247/foto_metod/Parca/" + Integer.toString(resimID) + ".jpg") .getContent()); resim.setImageBitmap(bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

这是我的最后一段代码:

public class ResimCek implements Runnable {

    int resimID = 0;

    public ResimCek(int parcaID) {
        // store parameter for later user
        resimID = parcaID;
    }

    public void run() {

        int resID = getResources().getIdentifier(Integer.toString(resimID) , "tag", getPackageName()); 
        ImageView resim = (ImageView) findViewById(resID);

        Drawable image = ImageOperations(getBaseContext(),"http://141.11.11.206/parca/" + Integer.toString(resimID) + ".jpg" ,"I" + Integer.toString(resimID) + ".jpg");
        // ******************************************************
        resim.setImageDrawable(image); // I GOT THE ERROR HERE!!!
    }
}
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
    try {
        InputStream is = (InputStream) this.fetch(url);
        Drawable d = Drawable.createFromStream(new URL(url).openConnection().getInputStream(),saveFilename);
        return d;
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}
谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您可能忘记在清单文件中声明互联网权限,请检查。

答案 1 :(得分:0)

下载FtpClient

使用用户名和密码登录。 查看this文档,希望它对您有所帮助。