1

jpegファイルのURLを取得するopenhttpconnectionから入力ストリームを取得すると、常にnullのビットマップリターンが返されます。たとえば、私のURL文字列は

 http://10.0.2.2:8000/mydirectory/myfile.jpg

ここで、8000は、エミュレーターで実行されているローカルサーバーのポートです。これと同じタイプのURLを使用して、Webサービスから取得できますが、画像ファイルを取得することはできません。jpgegファイルをビットマップにするために私がしなければならない何か違うことはありますか、それとも私のURLフォーマットに何か問題がありますか?以下は私のコードです。どんな助けでも大歓迎です。

  private Bitmap downloadImage(String url) {

        Bitmap bmap = null;
        InputStream inStream = null;

        try {
            inStream = openHttpConnection(url);
            Log.d("inStream", String.valueOf(inStream));
            bmap = BitmapFactory.decodeStream(inStream);
            if(inStream != null)
                inStream.close();

        }
        catch (IOException el) {
            el.printStackTrace();
        }
        return bmap;
    }

    private InputStream openHttpConnection(String urlString) throws IOException {

        InputStream inStream = null;
        int checkConn = -1;
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        try {

            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            checkConn = httpConn.getResponseCode();
            if (checkConn == HttpURLConnection.HTTP_OK) {
                inStream = httpConn.getInputStream();

            }

        }
        catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return inStream;
    }
4

0 に答える 0