facebook twitter hatena line email

「Android/base64」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(base64をdecodeして画像へ)
(base64をdecodeして画像へ)
行25: 行25:
  
 
参考:https://stackoverflow.com/questions/30167205/base64-decode-for-image-jpegbase64-in-android
 
参考:https://stackoverflow.com/questions/30167205/base64-decode-for-image-jpegbase64-in-android
 +
 +
画像をbase64に変換するwebサイト:https://lab.syncer.jp/Tool/Base64-encode/

2018年11月2日 (金) 14:44時点における版

base64をdecodeして画像へ

String encodedDataString = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAFD0lEQVRYR82Yf0yUdRzH348R7TRoHAcp5WiLCcpugkthLTZwMC4oB7ogHE2kMGFJp0k15iCojIFhQOJc/qDEEpRSnEDJIr3+gAqcMhKWrViTQAjaOe9aZ37aA9w9v597jgPn89/d89z3Xs/7+/m8P5/Pl8F9fjEsH9n7qSb3CFZUVCMldNH0d95ef9/4jez65Vim8/FqPYbsQ/RhVDh2DwG+2IgvR07juRDvIMneTZm6WDQDSN22D9u3ZyA5ejl8GcZjWKY8AlQyyOk1F8g/2z6h7vAtSH/Sd3ZHuilPF4vD/G1IeAOttcXYYAz0CJJ5BSDBQvBMyYnOMjIkluLNy1ZURvsrA87CJpR/hc+L07DMR5uaDE32kFkfgxpR0GlR0gnH/rTsshWlTkCrhUz+cfhaIZAXowCnhz9CSuiM4mrXzBvPAZIPJwGkCbIcq8HHue9Ox6HctQjRODb4PbZELFGFdN30BPLOtYPkszJf8L98BZ03iGw02H4U5pTXZNVkd+nceAuSg5S3W0CvFZIco9T00lK8eJJjlAN0gdp/pYYd67H18LBUzPh6jHTlI0QhwyXyaoW8S6PUnMVBqgFOhxE56MrBXEQVHJdAmhquoSNnpexWy345F0h3gE6qgfo0iiw4I4D0QSK+mLiAFwzSrVYMUE8gTzyzFAP7rahYN2MzrLotZZ8iYOtOJIoylWiCTqUakNEmEnJbO/49ZJKYuWoGsSWwLNmIdy4JFxNbELt9Dvi4Fieezey3jGFn3KPCWL/ZTk8FP4te3rJsVp8a6cOmEKGKzK48Mz1k0EnigrHZ8F9gIGxNJagbkMa2mk+ypY5fSer6bdhhXCyA/Lk6nlbt+k6wsKnhOjpywgTPsR/InVkq3VeCFAOyxnzx1gGs9ePUYVXO9I8T+uSGI5g6m4sAXkZ7BciCy0HK/XlobT+GC42c75KD2l5+EKlHuddn1/pmqgXxAdyLeA0oB8kmQlOWQeCTfihC951KRPJq8ERbIRlSalUNf14A5SDFPsk+U9VnQ9EaLhbp90Zinsi+N4BaIMVeSfY+ytOtEbRl4mfmTUGnDOKY5CtZM3Qbr4dzzQFNniFGnyZQUByr8w6opCRr5j/ut6J21syny5+Mgm//MImKdXpXMi0IoHzi2Mj6jw6P6LgMvXujmdY+liEw7PI+G0p4cbpggEoWxN/Pe5rFH3R0wGE2gT/fqEGy5bGz8HEk1Y25mNmm4fz4BUF/OG8KVvY7UBTWC7MuVtP4wJ/8XITx9RjvykcQv5JkYDWNJ+hlq9kDf3Wh86q2Qui0B61dkJyZxxwaRM+rEZJarEowUJ9NkQWNbikFQ5PGGYdvQWw30zjci82igwO3UxVLpgVSYsKeQtpO4PbZzVgiav01AWqBlB2aNEKyw9XULV/o/aXHJJoB3UEqtfxaY1IphjwCVINUnepuNhETnClh0HI44DGgEqQS4OjVFno/a5NsV67FzOcEKAcpyGKy0XVLKxqrSlF2bsitA6gpOWdAMaQnZzNyxEqQXgHyIQWAoqFJAJR9AD+9tQLHjUmyFefk8GfYGPqwoJtxuwXuHrhSXUCXkva5Jjfx0MT+nq2ze1trYH5+1fR4SpMWMuvjJJB7vh3De+u5MdVrBeXgydpDJv+YmQOj1emo2rMbOelPI0h0Jii2oGLLGPaKZuiFAXT8QRfP/4LgKCPCQg2qR79OSD+Rcs4XXxBAdyHhyf37HvB/p8f6RxVAzkUAAAAASUVORK5CYII=";
encodedDataString = encodedDataString.replace("data:image/png;base64,","");
byte[] imageAsBytes = Base64.decode(encodedDataString.getBytes(), 0);
// imageViewへ
ImageView imgView = findViewById(R.id.image1);
imgView.setImageBitmap(BitmapFactory.decodeByteArray(
               imageAsBytes, 0, imageAsBytes.length));
// imageButtonへ
ImageButton imgbtn = findViewById(R.id.imageButton1);
imgbtn.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeByteArray(
imageAsBytes, 0, imageAsBytes.length)));

レイアウト

 <ImageView
           android:id="@+id/image1"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />
<ImageButton
           android:id="@+id/imageButton1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="#00000000" />

参考:https://stackoverflow.com/questions/30167205/base64-decode-for-image-jpegbase64-in-android

画像をbase64に変換するwebサイト:https://lab.syncer.jp/Tool/Base64-encode/