如何将一张图片(最大200KB)转换成 Base64 字符串?
我需要知道在 Android 中如何实现这个功能,因为我需要在我的主要应用程序中添加上传图片到远程服务器的功能,并将它们作为字符串放入数据库的一行中。
我已经在 Google 和 Stack Overflow 上搜索了很久,但是没有找到易于理解的示例代码。虽然我找到了一些示例代码,但它们并没有讲述如何将图片转换成字符串。所以我需要将它们转换成字符串后通过 JSON 上传到远程服务器。
对于那些想要将图像文件转换为Base64字符串的人,而不需要压缩或先将文件转换为位图的高效方法,您可以选择将文件编码为base64。
val base64EncodedImage = FileInputStream(imageItem.localSrc).use {inputStream - >
ByteArrayOutputStream().use {outputStream - >
Base64OutputStream(outputStream, Base64.DEFAULT).use {
base64FilterStream - >
inputStream.copyTo(base64FilterStream)
base64FilterStream.flush()
outputStream.toString()
}
}
}
public String getBase64FromFile(String path)
{
Bitmap bmp = null;
ByteArrayOutputStream baos = null;
byte[] baat = null;
String encodeString = null;
try
{
bmp = BitmapFactory.decodeFile(path);
baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baat = baos.toByteArray();
encodeString = Base64.encodeToString(baat, Base64.DEFAULT);
}
catch (Exception e)
{
e.printStackTrace();
}
return encodeString;
}
为编码而用
private String encodeImage(Bitmap bitmap){
int previewWidth=150;
int previewHeight=bitmap.getHeight()*previewWidth/ bitmap.getWidth();
Bitmap previewBitmap = Bitmap.createScaledBitmap(bitmap,previewWidth,previewHeight,false);
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
previewBitmap.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);
byte[] bytes=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(bytes,Base64.DEFAULT);
}
private final ActivityResultLauncher<Intent> pickImage=registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode()==RESULT_OK){
if (result.getData()!=null){
Uri imageuri=result.getData().getData();
try{
InputStream inputStream= getContentResolver().openInputStream(imageuri);
Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
binding.imageProfile.setImageBitmap(bitmap);
encodedImage= encodeImage(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
}
);
private fun stringToBitmap(encodedString: String): Bitmap {
val imageBytes = decode(encodedString, DEFAULT)
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yyuyuyuuyuyuyu"
android:id="@+id/tv5"
/>
TextView textView5;
Bitmap bitmap;
textView5 = (TextView) findViewById(R.id.tv5);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// Get the Base64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
@Override
protected void onPostExecute(String s) {
textView5.setText(s);
}
}.execute();
byte[] decodedString = Base64.decode(result.getBytes(), Base64.DEFAULT);
我写了一个静态函数。我认为这样更有效率。
public static String file2Base64(String filePath) {
FileInputStream fis = null;
String base64String = "";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024 * 100];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
base64String = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
return base64String;
}
简单而更容易!
byte[] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);