最終追記
以下の内容は私の誤った認識の元で質問しておりました。
以下に記述している処理で文字化けは発生いたしません。
デバッグ実行している際に、 そもそもテキストではないファイルをテキスト表示したことで 文字化けが発生し画像データが壊れてしまったと誤った認識をしていました。
拝見していただきありがとうございます。
不足・不手際等ございましたら、ご指摘いただければと思います。
###前提・実現したいこと
ImageViewの画像をbyte[]形式に変換したいです。
前提・実現したいことの追記
行いたい処理としては、アプリの画像をWebページのAPIに送りたく、
下のサイトのようなマルチパートデータで送信するためにbyteに変換したいです。
http://chicketen.blog.jp/archives/1423162.html
###発生している問題・エラーメッセージ
ByteArrayOutputStream baos = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); // ↑この時点で文字化け発生 byte[] bytes = baos.toByteArray();
上記の ByteArrayOutputStream baosが
「������JFIF����������〜」のように文字化けしてしまいます。
文字コードをセットしなければいけないのだと思うのですが私では方法を見つけることができませんでした。
文字化けの対処方法をご教示していただきたいです。
よろしくお願いいたします。
###該当のソースコード
xml
1 <ImageView 2 android:layout_width="100dp" 3 android:layout_height="100dp" 4 android:id="@+id/imageView1" 5 android:layout_alignParentTop="true" 6 android:layout_alignParentStart="true" 7 android:src="@drawable/image1" /> 8 9 <Button 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="change" 13 android:id="@+id/button1" 14 android:layout_alignBottom="@+id/imageView1" 15 android:layout_centerHorizontal="true" 16 android:layout_marginBottom="29dp" /> 17
java
1MainActivity.java 2 3import android.graphics.Bitmap; 4import android.graphics.drawable.BitmapDrawable; 5import android.support.v7.app.AppCompatActivity; 6import android.os.Bundle; 7import android.view.View; 8import android.widget.ImageView; 9 10import java.io.ByteArrayOutputStream; 11 12public class MainActivity extends AppCompatActivity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 public void change(View view) { 21 ImageView iv1 = (ImageView) findViewById(R.id.imageView1); 22 Bitmap bmp = ((BitmapDrawable)iv1.getDrawable()).getBitmap(); 23 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 24 bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 25 byte[] bytes = baos.toByteArray(); 26 } 27}
###補足情報(言語/FW/ツール等のバージョンなど)
OS:Mac 10.11.5
開発環境:AndroidStudio 2.1
API:21(Android5.0)以上
回答3件
あなたの回答
tips
プレビュー