Android 远程图像错误

发布于 2024-12-11 08:06:29 字数 1746 浏览 0 评论 0原文

我想从远程服务器下载图像。但每次我都会遇到空指针异常。

连接服务器的方法

private InputStream OpenHttpConnection(String urlString)
    throws IOException
    {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                    
        throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();                
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();    
            Log.i("Download ", "Response: OK");
           }     
        else
            Log.i("Download ", "Response: NOK");
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");            
    }
    return in;    
}

创建位图的方法

private Bitmap DownloadImage(String URL)
    {        
        Bitmap bitmap = null;
        InputStream in = null;        
    try {
        in = OpenHttpConnection(URL);
        Log.i("Download ", "InputStream Available: " +in.available());
        bitmap = BitmapFactory.decodeStream(in);
        Log.i("Download ", "Bitmap: " +bitmap.describeContents());
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;                
}

当我decodeStream时,会抛出空指针异常,但是当我使用不同的URL时,它会起作用。

我在端口 90 上运行 Apache。如果有的话,这也会有影响吗?

I want to Download An image from a remote server. But each time I get A nullpointer exception.

Method For Conencting to Server

private InputStream OpenHttpConnection(String urlString)
    throws IOException
    {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))                    
        throw new IOException("Not an HTTP connection");

    try{
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();                
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();    
            Log.i("Download ", "Response: OK");
           }     
        else
            Log.i("Download ", "Response: NOK");
    }
    catch (Exception ex)
    {
        throw new IOException("Error connecting");            
    }
    return in;    
}

Method For Creating Bitmap

private Bitmap DownloadImage(String URL)
    {        
        Bitmap bitmap = null;
        InputStream in = null;        
    try {
        in = OpenHttpConnection(URL);
        Log.i("Download ", "InputStream Available: " +in.available());
        bitmap = BitmapFactory.decodeStream(in);
        Log.i("Download ", "Bitmap: " +bitmap.describeContents());
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;                
}

The null pointerException is thrown when I decodeStream, but when I use a different URL it works.

I run Apache on port 90. could this also have an effect if any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-12-18 08:06:29

试试这个我希望有效。

要与 ftp 连接,请使用此代码

public FTPClient mFTPClient = null;

public boolean ftpConnect(String host, String username,
                              String password, int port)
    {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);
                return status;
            }
        } catch(Exception e) {
            Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

下载文件,请使用此代码

public boolean ftpDownload(String srcFilePath, String desFilePath)
{
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {
        Log.d(TAG, "download failed");
    }

    return status;
}

try this I hope is working.

to connect with ftp use this code

public FTPClient mFTPClient = null;

public boolean ftpConnect(String host, String username,
                              String password, int port)
    {
        try {
            mFTPClient = new FTPClient();
            // connecting to the host
            mFTPClient.connect(host, port);

            // now check the reply code, if positive mean connection success
            if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                // login using username & password
                boolean status = mFTPClient.login(username, password);
                return status;
            }
        } catch(Exception e) {
            Log.d(TAG, "Error: could not connect to host " + host );
        }

        return false;
    }

to download file use this code

public boolean ftpDownload(String srcFilePath, String desFilePath)
{
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {
        Log.d(TAG, "download failed");
    }

    return status;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文