简体   繁体   English

如何从InputStream获取Base64字符串?

[英]How to get Base64 String from InputStream?

I'm on a problem by taking the selected gallery picture and want to save it first as Base64 String in a XML file (for later use. For example if you exit the app and open it again). 我在拍摄所选的画廊图片时遇到问题,想先将其另存为XML文件中的Base64 String (以备后用。例如,如果退出应用程序并再次打开它)。

As you can see I get the Image on a InputStream 如您所见,我在InputStream上获得了图像

But first of all the onClick method: 但首先是onClick方法:

public void onClick(DialogInterface dialog, int which) {
                    pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    pictureActionIntent.setType("image/*");
                    startActivityForResult(pictureActionIntent,GALLERY_PICTURE);
                }

Now in the onActivityResult method I want to store the image from InputStream to Base64 String . 现在在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 This is what I do after creating the base64 String. @EDIT这是创建base64字符串后的工作。

Bitmap bmp = base64EncodeDecode.decodeBase64(Items.get("image"));
Image1.setImageBitmap(bmp);

And this is the decoding Method: 这是解码方法:

    public Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, Base64.DEFAULT);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

I tried to use Base64InputStream but without success. 我尝试使用Base64InputStream但没有成功。 Can you give me a hint how to get from InputStream to Base64 String ? 您能给我一个提示,如何从InputStreamBase64 String吗?

How many steps it will take doesn't matter. 它将采取多少步骤无关紧要。

I hope someone can help me! 我希望有一个人可以帮助我!

Kind Regards! 亲切的问候!

Write these lines in onActivityResult method 将这些行写在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;
}

you can use base64 String of image. 您可以使用base64图片字符串。

Also don't forget to add permissions in AndroidManifest.xml file READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE 同样不要忘记在AndroidManifest.xml文件READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE添加权限

EDIT:: Decode base64 to bitmap 编辑::解码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)
);

Hope it'll work. 希望它能工作。

This should work: 这应该工作:

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

Got this code from something I use in an application, stripped it down to the most basic as far as i know. 从我在应用程序中使用的东西得到了这些代码,据我所知,它被简化为最基本的代码。

If you are selecting image from Gallery then why you are saving it as Base64 string in xml file , you can reuse that image from gallery . 如果要从库中选择图像,那么为什么要将其另存为xml文件中的Base64字符串,则可以从库中重用该图像。

For this save image url in SharedPreferences and use that url again to show image . 为此,将图像URL保存在SharedPreferences中,然后再次使用该URL显示image。

Edit : 编辑:

If you want to store it locally then you can use SQLite Database to store it , for more detail visit this link . 如果要将其存储在本地,则可以使用SQLite数据库进行存储,有关更多详细信息,请访问链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM