How to convert a file to Base64?

后端 未结 7 1396
遥遥无期
遥遥无期 2020-12-06 09:58

Here the report contain the path(pathname in sdcard in string format)

File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, rep         


        
7条回答
  •  忘掉有多难
    2020-12-06 10:45

    To convert a file to Base64:

    File imgFile = new File(filePath);
    if (imgFile.exists() && imgFile.length() > 0) {
        Bitmap bm = BitmapFactory.decodeFile(filePath);
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bOut);
        String base64Image = Base64.encodeToString(bOut.toByteArray(), Base64.DEFAULT);
    }
    

提交回复
热议问题