2016-06-23 9 views
1

ビットマップをJSONオブジェクトの一部として送信するためにビットマップを文字列にエンコードします。イメージの受信者は文字列を取得し、イメージに再度変換します。BitmapFactory.decodeByteArrayを使用してビットマップを文字列に変換してからビットマップに戻すことができません。

これがすべて関連している場合は、Androidアプリでこれを実行しています。

ビットマップを文字列に変換するのはうまくいくようですが、文字列をビットマップに変換すると、NullPointerExceptionが発生します。

は、私は基本的にこれを煮詰めることを試みた(と文字列にバックテストのために、同じ方法でビットマップに変換する)ので、私は以下の持っている:

public static void convertBitmapToBase64String(Context context, String filename, int maxStringSize) 
{ 
    Bitmap originalBmp = PicUtils.getBitmapFromFilename(filename, null, -1); 
    String base64Image = PicUtils.convertBitmapToBase64StringFromFile(context, TEMP_FILENAME); 

    // The encoded string is not null, so encoding seems to work. 
    DataUtils.log("base64Image length is " + base64Image.length()); 

    // Test if we can convert back 
    final byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT); 

    // This returns null! Is failing here. 
    Bitmap decodedByteBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
} 

public static Bitmap getBitmapFromFilename(String filename) 
{ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filename, options); 
    Bitmap finalBitmap = BitmapFactory.decodeFile(filename); 
    return finalBitmap; 
} 

public static String convertBitmapToBase64StringFromFile(Context context, String filename) 
{ 
    try { 
     FileInputStream fis = context.openFileInput(filename); 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     Base64OutputStream outputStream = new Base64OutputStream(byteArrayOutputStream, Base64.DEFAULT); 
     IOUtils.copy(fis, outputStream); 
     return new String(byteArrayOutputStream.toByteArray(),"UTF-8"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     DataUtils.log(e.getMessage()); 
     return null; 
    } 
} 

任意のヒントがあります感謝!

+0

'base64EncodedImg'を作成して無視します。それ以外のものは存在しない 'base64Image'に基づいています。 – CommonsWare

+0

こんにちは@CommonsWare、申し訳ありませんが、私はここに貼り付けるコードを少しきれいにしていたときにコピー/ペーストエラーでした。変数を更新しました。私はあなたの助けを感謝します – zundi

+0

'新しいString(byteArrayOutputStream.toByteArray()、UTF-8") 'と' Base64.decode(base64Image、Base64.DEFAULT) 'は逆の操作であるとは幾分懐疑的です。 'decode()'の後の 'byte []'の長さと内容が元のものと同じかどうかを確認してください。 – CommonsWare

答えて

関連する問題