如何从 InputStream 获取 Base64 字符串?
How to get Base64 String from InputStream?
我在拍摄选定的图库图片时遇到了问题,我想先将其另存为 Base64
String
在 XML 文件中(供以后使用。例如,如果您退出应用程序并再次打开它)。
如您所见,我在 InputStream
上获取图像
但首先是 onClick
方法:
public void onClick(DialogInterface dialog, int which) {
pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT);
pictureActionIntent.setType("image/*");
startActivityForResult(pictureActionIntent,GALLERY_PICTURE);
}
现在在 onActivityResult
方法中我想将图像从 InputStream
存储到 Base64
String
.
case GALLERY_PICTURE:
if (resultCode == RESULT_OK && null != data) {
InputStream inputstream = null;
try {
inputstream = getApplicationContext().getContentResolver().openInputStream(data.getData());
Base64InputStream in = new Base64InputStream(inputstream,0);
} catch (IOException e) {
e.printStackTrace();
}
@EDIT
这是我在创建 base64 字符串后所做的。
Bitmap bmp = base64EncodeDecode.decodeBase64(Items.get("image"));
Image1.setImageBitmap(bmp);
这是解码方法:
public Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
我尝试使用 Base64InputStream
但没有成功。
你能告诉我如何从 InputStream
到 Base64
String
吗?
走多少步并不重要。
希望有人能帮帮我!
亲切的问候!
这应该有效:
public static byte[] getBytes(Bitmap bitmap) {
try{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.flush();
//bitmap.compress(CompressFormat.PNG, 98, stream);
bitmap.compress(CompressFormat.JPEG, 98, stream);
//bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
return stream.toByteArray();
} catch (Exception e){
return new byte[0];
}
}
public static String getString(Bitmap bitmap){
byte [] ba = getBytes(bitmap);
String ba1= android.util.Base64.encodeToString(ba, android.util.Base64.DEFAULT);
return ba1;
}
从我在应用程序中使用的代码中获取此代码,据我所知将其简化为最基本的代码。
在onActivityResult
方法中写下这些行
try {
// get uri from Intent
Uri uri = data.getData();
// get bitmap from uri
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// store bitmap to file
File filename = new File(Environment.getExternalStorageDirectory(), "imageName.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);
out.flush();
out.close();
// get base64 string from file
String base64 = getStringImage(filename);
// use base64 for your next step.
} catch (IOException e) {
e.printStackTrace();
}
private String getStringImage(File file){
try {
FileInputStream fin = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
fin.read(imageBytes, 0, imageBytes.length);
fin.close();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
} catch (Exception ex) {
Log.e(tag, Log.getStackTraceString(ex));
toast("Image Size is Too High to upload.");
}
return null;
}
你可以使用 base64
图片字符串。
另外不要忘记在 AndroidManifest.xml
文件 READ_EXTERNAL_STORAGE
和 WRITE_EXTERNAL_STORAGE
中添加权限
编辑::将base64解码为位图
byte[] bytes = Base64.decode(base64.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
);
希望它能奏效。
如果您从图库中选择图像,那么为什么要将其保存为 xml 文件中的 Base64 字符串,您可以重复使用图库中的图像。
为此,在 SharedPreferences 中保存图像 url 并再次使用该图像 url 来显示图像。
编辑:
如果您想将其存储在本地,则可以使用 SQLite 数据库来存储它,有关更多详细信息,请访问 this link。
我在拍摄选定的图库图片时遇到了问题,我想先将其另存为 Base64
String
在 XML 文件中(供以后使用。例如,如果您退出应用程序并再次打开它)。
如您所见,我在 InputStream
但首先是 onClick
方法:
public void onClick(DialogInterface dialog, int which) {
pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT);
pictureActionIntent.setType("image/*");
startActivityForResult(pictureActionIntent,GALLERY_PICTURE);
}
现在在 onActivityResult
方法中我想将图像从 InputStream
存储到 Base64
String
.
case GALLERY_PICTURE:
if (resultCode == RESULT_OK && null != data) {
InputStream inputstream = null;
try {
inputstream = getApplicationContext().getContentResolver().openInputStream(data.getData());
Base64InputStream in = new Base64InputStream(inputstream,0);
} catch (IOException e) {
e.printStackTrace();
}
@EDIT 这是我在创建 base64 字符串后所做的。
Bitmap bmp = base64EncodeDecode.decodeBase64(Items.get("image"));
Image1.setImageBitmap(bmp);
这是解码方法:
public Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
我尝试使用 Base64InputStream
但没有成功。
你能告诉我如何从 InputStream
到 Base64
String
吗?
走多少步并不重要。
希望有人能帮帮我!
亲切的问候!
这应该有效:
public static byte[] getBytes(Bitmap bitmap) {
try{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.flush();
//bitmap.compress(CompressFormat.PNG, 98, stream);
bitmap.compress(CompressFormat.JPEG, 98, stream);
//bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
return stream.toByteArray();
} catch (Exception e){
return new byte[0];
}
}
public static String getString(Bitmap bitmap){
byte [] ba = getBytes(bitmap);
String ba1= android.util.Base64.encodeToString(ba, android.util.Base64.DEFAULT);
return ba1;
}
从我在应用程序中使用的代码中获取此代码,据我所知将其简化为最基本的代码。
在onActivityResult
方法中写下这些行
try {
// get uri from Intent
Uri uri = data.getData();
// get bitmap from uri
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// store bitmap to file
File filename = new File(Environment.getExternalStorageDirectory(), "imageName.jpg");
FileOutputStream out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);
out.flush();
out.close();
// get base64 string from file
String base64 = getStringImage(filename);
// use base64 for your next step.
} catch (IOException e) {
e.printStackTrace();
}
private String getStringImage(File file){
try {
FileInputStream fin = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
fin.read(imageBytes, 0, imageBytes.length);
fin.close();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
} catch (Exception ex) {
Log.e(tag, Log.getStackTraceString(ex));
toast("Image Size is Too High to upload.");
}
return null;
}
你可以使用 base64
图片字符串。
另外不要忘记在 AndroidManifest.xml
文件 READ_EXTERNAL_STORAGE
和 WRITE_EXTERNAL_STORAGE
编辑::将base64解码为位图
byte[] bytes = Base64.decode(base64.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
image.setImageBitmap(
BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
);
希望它能奏效。
如果您从图库中选择图像,那么为什么要将其保存为 xml 文件中的 Base64 字符串,您可以重复使用图库中的图像。
为此,在 SharedPreferences 中保存图像 url 并再次使用该图像 url 来显示图像。
编辑:
如果您想将其存储在本地,则可以使用 SQLite 数据库来存储它,有关更多详细信息,请访问 this link。