1

URLから画像をダウンロードしようとしています。画像の種類は PNG で、解像度は 400x400 ピクセルです。

ダウンロード コードのスニペットは次のとおりです。

Bitmap bitmap=null;
URL imageUrl = new URL(url);
conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream ins=conn.getInputStream();
os = new FileOutputStream(f);
Utilities.getUtilities().copyStream(ins, os);
os.flush();

Log.i(TAG_NAME, "file size : "+ f.length());
Log.i(TAG_NAME, "file exists in cache? " + f.exists());
bitmap = decodeFile(f);
return bitmap;

これがファイルライターです。

public void copyStream(InputStream is, OutputStream os) {
  final int buffer_size=1024;
    try
    {
        byte[] bytes=new byte[buffer_size];
        for(;;)
        {
          int count=is.read(bytes, 0, buffer_size);
          if(count==-1)
              break;
          os.write(bytes, 0, count);
        }
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}

そしてデコード方法

private Bitmap decodeFile(File f){
   //decode image size
   BitmapFactory.Options o = new BitmapFactory.Options();
   o.inJustDecodeBounds = true;
   try {
     BitmapFactory.decodeStream(new FileInputStream(f));
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   }

   final int REQUIRED_SIZE = 400; //for testing, it is set to b a constant
   System.out.println("REQUIRED_SIZE >>> " + REQUIRED_SIZE);
   int width_tmp=o.outWidth, height_tmp=o.outHeight;
   int scale=1;
   while(true){
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
            break;
      width_tmp/=2;
      height_tmp/=2;
        scale*=2;
    }

    //decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    //o2.inJustDecodeBounds = true;
    o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
    o2.inSampleSize=scale; //scale is set off since android:src automatically scales the image to fit the screen

    try {
        return BitmapFactory.decodeStream(new FileInputStream(f));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

ファイルがデバイスに存在することがわかります。ただし、デコード ストリームは失敗しています。私はインターネットで何時間も検索しました。ほとんどすべてを試しましたが、成功せず、ほとんど頭が転がっていました。

ストリームをデコードすると、次のエラーが発生します。

SkImageDecoder::Factory returned null

ここに何か足りないものはありますか?

編集:

問題は解決しました。サーバーは、私が添付できなかった Cookie の詳細を期待していました。茂みの周りを叩いて、ほぼ一日を過ごしました:-)

皆様、貴重なコメントありがとうございます!

4

1 に答える 1

0

IMO、httpurlconn とネイティブのhttpclient実装のメリットを再評価することをお勧めします。Android/Google は httpurlconn を採用していますが、多くの場合、ネット プロトコルを取り巻く低レベルの詳細をより詳細に制御することを選択しています。

ビットマップ ハンドラーでラップするasync httpclient のサンプルを次に示します。method=processBitmapEntity()bmp サイズに影響するルールを使用して、サンプルを簡単に拡張できます。

getbitmap URL の例:

  public  int getBitmap(String mediaurl, int ctr){

           Handler handler = new Handler() {
               public void handleMessage(Message message) {
                 switch (message.what) {
                 case HttpConnection.DID_START: {
                   Log.d(TAG, "Starting connection...");
                   break;
                 }
                 case HttpConnection.DID_SUCCEED: {
                     //message obj is type bitmap
                     Log.d(TAG, "OK bmpARRAY " +message.arg1); 
                    Bitmap response = (Bitmap) message.obj;
                   break;
                 }
                 case HttpConnection.DID_ERROR: {

                   Exception e = (Exception) message.obj;
                   e.printStackTrace();
                   Log.d(TAG, "Connection failed.");
                   break;
                 }
               }
             }
           };        
           new HttpConnection(handler, PreferenceManager.getDefaultSharedPreferences(this), ctr).bitmap(mediaurl);

       return -1;  

上記のリンク サンプルの一部である HttpConnection クラスのビットマップ ハンドラー:

private void processBitmapEntity(HttpEntity entity) throws IOException {
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
    handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm));
}

そしてgitプロジェクト

于 2013-03-13T15:21:22.000 に答える