2011-10-21 10 views
0

リモートサーバーから画像をダウンロードします。しかし、私はnullpointer例外を得るたびに。ビットマップAndroidリモート画像ERROR

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ときヌルpointerExceptionがスローされ

が、私は別のURLを使用する場合、それが動作を作成するためのサーバーへのConenctingについては

方法

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

方法。

私はポート90でApacheを実行します。これが有効な場合は、これも有効ですか?

答えて

1

これを試してみてくださいこのコード

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

@Simsファイルの使用をダウンロードするには、このコード

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

を使用し、FTPに接続する

はそのコードあなたと罰金仕事やないですか? – Nik88

+0

あまりにも長い時間をかけて申し訳ありませんが、私は問題を理解しました。これはJPEG画像ではなくPNG画像をアップロードするときに機能します。しかし、私はあなたのコードを使用してアップロードします。 – Sims

+0

答えがあなたに役立つ場合は@Sims回答の左側にある右矢印をチェックすることができます。 – Nik88

関連する問題