2017-05-06 4 views
0

MySQLデータベースにイメージのbyteArrayを保存し、Databsesから取得して文字列をbyteArrayに、次にbyteArrayをビットマップに取得しています。しかし、ビットマップはヌルです。私は多くのコードを試しましたが、まだNULLです。 保存液で正確なエラーを取得する専門家を探している画像ImageViewのビットマップは、Android(MySQLデータベース)のヌルです

imgData=result; 
    byte[] byteArray = Base64.decode(result, Base64.DEFAULT); 
    Bitmap bMap = null; 
    bMap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length); 
    testimg.setImageBitmap(bMap); 

を取得する画像

private String imageviewtobyte(ImageView view){ 
     Bitmap bitmap=((BitmapDrawable) view.getDrawable()).getBitmap(); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byteArray = stream.toByteArray(); 
     ConvertImage = Base64.encodeToString(byteArray, Base64.DEFAULT); 
     return ConvertImage; 

    } 

+0

ConvertImageには文字列形式の画像が含まれていますか? –

+0

'画像を保存する '。そのコードは画像をアップロードせず、MySQLデータベースのどこかに保存しません。 – greenapps

+0

私はここに具体的なコード値だけが右に来て右に来ていることを与えている。私はデバッグによってチェックしたが、問題はビットマップ変換が動作していないことです –

答えて

0
//check result object it is null or not 
if(result != null){ 
    imgData = result; 
    Bitmap bitmap = StringToBitMap(imgData); 
    if(bitmap != null){ 
     testimg.setImageBitmap(bitmap); 
    } 
} 

/** 
* @param encodedString 
* @return bitmap (from given string) 
*/ 
public static Bitmap StringToBitMap(String encodedString){ 
    try{ 
     byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT); 
     Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); 
     return bitmap; 
    }catch(Exception e){ 
     e.getMessage(); 
     return null; 
    } 
} 
+1

私はすでに結果がnullではないことをチェックしましたが、あなたのメソッドを実装することでこれをチェックさせてください –

+0

あなたのStringが有効なbsae64であるかどうかを確認するには、 .askapache.com/online-tools/base64-image-converter/ –

+0

このコードはOPのコードと同じです。また、ビットマップがnullの場合は、それをイメージビューに割り当てることができます。 – greenapps

0

Base64Stringは大きなビットマップではうまく機能しません。まず、ビットマップをbase64Stringに変換する前に、ビットマップのサイズを小さくする必要があります。私は、ビットマップのサイズを小さくするためのコードを添付しました。 このコードではmaxWidth = 960.0とmaxheight = 1280.0を使用できます。

public Bitmap GetBitmap(Bitmap finalimage) { 
    int actualHeight = finalimage.getHeight(); 
    int actualWidth = finalimage.getWidth(); 
    float imgRatio = actualWidth/actualHeight; 
    float maxRatio = maxWidth/maxHeight; 

    if (actualHeight > maxHeight || actualWidth > maxWidth) { 
     if (imgRatio < maxRatio) { 
      imgRatio = maxHeight/actualHeight; 
      actualWidth = (int) (imgRatio * actualWidth); 
      actualHeight = (int) maxHeight; 
     } else if (imgRatio > maxRatio) { 
      imgRatio = maxWidth/actualWidth; 
      actualHeight = (int) (imgRatio * actualHeight); 
      actualWidth = (int) maxWidth; 
     } else { 
      actualHeight = (int) maxHeight; 
      actualWidth = (int) maxWidth; 
     } 
    } 
    finalimage = Bitmap.createScaledBitmap(finalimage, actualWidth, actualHeight, false); 
    return finalimage; 
} 
+0

'Base64Stringは大きなビットマップでうまく機能しません。 '。 ???ビットマップの場合は?これはバイト配列でのみ使用されます。配列の制限について聞いたことはありません。 – greenapps

+0

それは変だけど、それは本当です。 [this](http://stackoverflow.com/questions/28641704/converting-bitmap-to-base64-string-causes-outofmemory-error)のリンクを確認することができます。 –

+0

サイズは問題ではありませんdhruv gangani私はこれをチェックしました –

関連する問題