有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试着选择一个图像的路径,比如: "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这就是我要传递给方法的东西。有人能帮我吗


共 (1) 个答案

  1. # 1 楼答案

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

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