如何从输入流获取 Base64 字符串



我在拍摄选定的图库图片时遇到了问题,并希望先将其保存为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但没有成功。你能给我一个提示,如何从InputStreamBase64 String吗?

需要多少步骤并不重要。

希望有人能帮到我!

亲切问候!

在方法

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图像字符串。

另外,不要忘记在文件READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE中添加权限AndroidManifest.xml

编辑:: 解码 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)
);

希望它会起作用。

这应该有效:

    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;
}

从我在应用程序中使用的代码中获取此代码,据我所知,将其剥离到最基本的代码。

如果您从图库中选择图像,那么为什么要将其另存为 xml 文件中的 Base64 字符串,您可以从图库中重用该图像。

对于此在共享首选项中保存图像 url,然后再次使用该 URL 显示图像 。

编辑:

如果要将其存储在本地,则可以使用SQLite数据库来存储它,有关更多详细信息,请访问此链接。

最新更新