如何从InputStream获取Base64字符串?

时间:2016-02-09 11:00:02

标签: java android string 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

它将采取多少步骤并不重要。

我希望有人可以帮助我!

亲切的问候!

3 个答案:

答案 0 :(得分:3)

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_STORAGEWRITE_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)
);

希望它能运作。

答案 1 :(得分:0)

这应该有效:

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

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

答案 2 :(得分:0)

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

对于SharedPreferences中的此保存图片网址,再次使用该网址显示图片。

修改:

如果您想在本地存储它,那么您可以使用SQLite数据库来存储它,有关详细信息,请访问this链接。