Take image from gallery and convert to base64 issue

前端 未结 4 1463
醉话见心
醉话见心 2021-01-03 14:13

sorry for asking silly question.but it solved yet my me.please help i had tried all codes on stackoverflow and follow other tutorial but it wont help at all.i am taking imag

4条回答
  •  无人及你
    2021-01-03 14:58

    You can use following code:

    public static String encodeTobase64(Bitmap image) {
        Bitmap immagex=image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
    
        Log.e("LOOK", imageEncoded);
        return imageEncoded;
    }
    

    Use below code in onActivityResult()

     InputStream imageStream = null;
            try {
                imageStream = this.getContentResolver().openInputStream(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
            encodeTobase64(yourSelectedImage);
    

提交回复
热议问题