0

我正在尝试采用如下图像的路径:“content://media/external/images/media/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 是我传递给该方法的内容。谁能帮我?

4

1 回答 1

0

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

URL url = new URL(imagePath);
Bitmap bmt = BitmapFactory.decodeStream(url.openStream());
于 2012-08-15T17:27:12.043 回答