获取位图图像的路径并将其转换为Base64

时间:2012-08-15 17:10:30

标签: java android

我正试图拍摄像这样的图像路径: “内容://媒体/外部/图像/媒体/ 3”

并将其转换为base64字符串。

以下是我现在的代码:

public String ConvertandSetImagetoBase64(String imagePath) { 
    String base64 = null;
    byte[] input = null;

    try{
        FileInputStream fd = new FileInputStream(imagePath);
        Bitmap bmt = BitmapFactory.decodeFileDescriptor(fd.getFD());

        try{                
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            Bitmap tmp = ProfileActivity.scaleDownBitmap(bmt, 10, this);
            tmp.compress(Bitmap.CompressFormat.JPEG, 10, stream);
            input = stream.toByteArray();
            base64 = Base64.encodeToString(input, Base64.DEFAULT);
            //LocalProfileActivity.input = input;
        }catch(Exception e){
            Log.e(LOG_TAG,"[ONACTIVITYRESULT] Could not bind input to the bytearray: " + e.getMessage());
        }
    }
    catch (Exception e){
        Log.e("LocalProfile", "ConvertandSetImagetoBase64: Could not load selected profile image");
    }
    return base64;
}

content:// media / external / images / media / 3是我传入方法的内容。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

由于您将位置指定为URI,因此您可以尝试以下方法:

URL url = new URL(imagePath);
Bitmap bmt = BitmapFactory.decodeStream(url.openStream());