Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谁能告诉我如何将任何格式的图像直接转换为字符串?_Android_String_Base64 - Fatal编程技术网

Android 谁能告诉我如何将任何格式的图像直接转换为字符串?

Android 谁能告诉我如何将任何格式的图像直接转换为字符串?,android,string,base64,Android,String,Base64,您可以使用Base64 Android类: 不过,您必须将图像转换为字节数组。例如: 你有档案。为什么要将其膨胀为位图,然后再次压缩?这不仅浪费了时间和内存,而且还增加了压缩问题。只需将文件作为字节流读入即可。这种方法是否不适合实现目标???你能分享一些代码吗,这有助于我理解这个概念检查这个@V-rundPuro-hit是的我检查了你的链接但仍然找不到结果是的,我正在做同样的事情,但仍然没有得到结果,实际上,它对JPEG图像运行得很好,但我想让它对所有格式都可行。因此,您要将JPEG和png图像

您可以使用Base64 Android类:

不过,您必须将图像转换为字节数组。例如:


你有档案。为什么要将其膨胀为位图,然后再次压缩?这不仅浪费了时间和内存,而且还增加了压缩问题。只需将文件作为字节流读入即可。这种方法是否不适合实现目标???你能分享一些代码吗,这有助于我理解这个概念检查这个@V-rundPuro-hit是的我检查了你的链接但仍然找不到结果是的,我正在做同样的事情,但仍然没有得到结果,实际上,它对JPEG图像运行得很好,但我想让它对所有格式都可行。因此,您要将JPEG和png图像转换为字符串,或者将JPEG、png、BMP、Gif全部转换为字符串我要将所有图像格式[JPEG、png、BMP、Gif]转换为字符串。有什么具体的方法吗???
private String getBase64String() {

    // give your image file url in mCurrentPhotoPath
    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    // In case you want to compress your image, here it's at 40%

// here i use JPEG image but now can anyone tell how can i convert any format image in String 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
String ecodImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
Bitmap btm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
btm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //btm is the bitmap object   
byte[] b = baos.toByteArray();