2017-02-17 13 views
0

私はオンラインサイトを通じてbase64に画像を変換しました。 私はこのlinkを通ってStringにbase64文字列を保持しました。 (38、36)エラー:しかし、私は エラーというエラー取得長すぎる文字列定数をAndroidのbase64文字列を画像に変換する

私は

+0

あなたのコードを表示... –

答えて

2
 //encode image(from image path) to base64 string 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       Bitmap bitmap = BitmapFactory.decodeFile(pathOfYourImage); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
       byte[] imageBytes = baos.toByteArray(); 
       String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT); 

    //encode image(image from drawable) to base64 string 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourDrawableImage); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
       byte[] imageBytes = baos.toByteArray(); 
       String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT); 
1

は、使用してみましたアンドロイドの画像(ビットマップ)にBASE64を変換する方法を教えてくださいBitmapFactoryクラス?

このような何かを試してみてください:

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

はまた、あなたが得るエラーによると、あなたがあなたのbase64エンコード文字列を保持するために、静的な最終文字列を使用しているようです。 Javaでは、定数文字列の長さは64kに制限されています。

1

すべての文字列をチェックし

http://codebeautify.org/base64-to-image-converter

の最初の変換するには、この方法を試してみてください。

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ImageView image =(ImageView)findViewById(R.id.image); 

     //encode image to base64 string 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
     byte[] imageBytes = baos.toByteArray(); 
     String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT); 

     //decode base64 string to image 
     imageBytes = Base64.decode(imageString, Base64.DEFAULT); 
     Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 
     image.setImageBitmap(decodedImage); 
    } 
} 

http://www.thecrazyprogrammer.com/2016/10/android-convert-image-base64-string-base64-string-image.html

2

あなたは基本的に他のいくつかの組み込みメソッドを使用してコードを元に戻すことができます。

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT); 

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
関連する問題