如何将照片从SD卡转换为base64?

时间:2018-03-20 07:20:42

标签: java android image base64 sd-card

我希望将照片转换为base64,照片来自我的外部SD卡。我试过这段代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picture);

    File yourFile = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp/Hello.jpg");
    Bitmap bm = BitmapFactory.decodeFile(String.valueOf(yourFile));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    byte[] byteArrayImage = baos.toByteArray();

    String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

    TextView textView = (TextView) findViewById(R.id.simpleTextView);

    textView.setText(encodedImage); //set text for text view

}

但是在logcat中显示 bm.compress(Bitmap.CompressFormat.JPEG,40,baos); 有错误,有人可以分享我的想法吗?

logcat错误:

enter image description here

0 个答案:

没有答案