2016-02-09 123 views
2

我遇到了一个问题,通过选择图库图片并将其保存为Base64String保存在一个XML文件中(以备后用,例如,如果退出应用程序并再次打开它)。如何从InputStream获取Base64字符串?

正如你可以看到我坐上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但没有成功。 你能给我一个提示如何从InputStreamBase64String

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

我希望有人能帮助我!

亲切的问候!

回答

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

希望它会工作。

+0

太棒了!感谢你的代码!你能告诉我什么'format.format(新日期...'是? – raymondis

+0

ohhh。用于将当前日期格式化为'dd_MM_yyyy_hh_mm_ss_aa'这种格式。我将编辑答案。 – ELITE

+0

哦,是的,没关系。谢谢! – raymondis

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

得到了我在应用程序中使用的东西这个代码,剥离下来的最基本据我所知。

+0

但是如何从InputStream到Bitmap? – raymondis

0

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

为此将图像保存在SharedPreferences中,并再次使用该URL来显示图像。

编辑:

如果你想以本地存储它,那么你可以使用SQLite数据库来存放它,为更多详情,请this链接。

+0

这是我的第一个解决方案。但是如果用户删除图像?我想将它保存在xml中使其独立于原始文件 – raymondis

+0

@raymondis看到更新的答案。 –

相关问题