2016-06-28 28 views
5

私は1.5kbから9Mbまでのいくつかの写真を持つサーバーを持っています。 PC、タブレット、携帯電話からの写真。サーバーはBase64文字列にエンコードし、Androidクライアントに送信します。 BitmapFactory.decodeByteArrayでデコードすると、1つの300kbの写真がnullを返します...しかし、それは有効な画像であり、オンラインデコーダで良好にデコードされています。 ?!私は答えを見つけることができない2日間Android BitmapFactoryはBase64デコードされたバイト配列でnullを返します

byte[] decodedString = Base64.decode(image64, Base64.DEFAULT); 
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, ecodedString.length); 

任意のアイデアのおかげ

PS

private boolean decodeImage64(String uid, String image64, String name) { 
    Bitmap decodedByte; 
    boolean result = false; 
    if (image64 != null && !image64.isEmpty()) { 

     try { 
      Log.e(TAG, "decodeImage64: image64.getBytes().length = " + image64.getBytes().length); 
      byte[] decodedString = Base64.decode(image64, Base64.DEFAULT); 
      Log.e(TAG, "decodeImage64: decodedString = " + decodedString + " , decodedString.length = " + decodedString.length); 
      decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
      Log.e(TAG, "decodeImage64: decodedByte = " + decodedByte); 

      if (decodedByte != null) { 
       FileOutputStream out = null; 
       try { 
        out = new FileOutputStream(getImageFolderName() + "/" + uid + ".png"); 
        decodedByte.compress(Bitmap.CompressFormat.PNG, 100, out); 
        decodedByte.recycle(); 
        out.close(); 

       } catch (Exception e) { 
        Log.e(TAG, Log.getStackTraceString(e)); 
       } finally { 
        try { 
         if (out != null) { 
          out.close(); 
         } 
         if (decodedByte != null){ 
          decodedByte.recycle(); 
         } 
        } catch (IOException e) { 
         Log.e(TAG, Log.getStackTraceString(e)); 
        } 
       } 
       result = true; 
      }else { 
       Log.e(TAG, " !!!!!!!!!!!!!!!!!!!!!!! decodeImage64: decodedByte = null " + name); 
      } 
     }catch (Exception e){ 
      Log.e(TAG, Log.getStackTraceString(e)); 
     } 
    } else { 
     Log.e(TAG, "decodeImage64: image = null " + name); 
    } 
    return result; 
} 

そしてlogcat

良好な画像:

06-29 02:33:57.465 18197-18584/cps.agrovisio E/myLogs: ------------------------- doInBackground: Good photo 
06-29 02:34:13.993 18197-18584/cps.agrovisio E/myLogs: decodeImage64: image64.getBytes().length = 2264744 
06-29 02:34:14.085 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedString = [[email protected] , decodedString.length = 1676499 
06-29 02:34:14.635 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedByte = [email protected] 

悪いイメージ:

06-29 02:33:56.041 18197-18584/сps.agrovisio E/myLogs: ------------------------- doInBackground: Bad photo 
06-29 02:33:57.177 18197-18584/cps.agrovisio E/myLogs: decodeImage64: image64.getBytes().length = 372570 
06-29 02:33:57.194 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedString = [[email protected] , decodedString.length = 275799 
06-29 02:33:57.245 18197-18584/cps.agrovisio E/myLogs: decodeImage64: decodedByte = null 
+0

どのようなサイズの画像を区別しますか? – goto

+0

私の友人はあなたのイメージを試してもらえますか? –

+0

すべての写真jpg。 Androidタブレットからの問題 –

答えて

0

これは、あなたが探している答えではないかもしれないが、あなたは、フレームワークを使用して検討していますか?私はピカソを使用していて、それは簡単です。 Picasso.with(context).load( "http://i.imgur.com/DvpvklR.png").into(imageView);

http://square.github.io/picasso/

+0

ありがとう!しかし、私はピカソを使うことはできません。私はjsonでいくつかのパラメータを取得しました。 –

0

スライスimage64から一部data:image/jpg;base64,。コード化された文字列のみを持ちます。

これは部分文字列メソッドを使用すると動作します。

関連する問題