Android 附件/文件选择器到 InputStream 字符串

Android attachment / file picker into InputStream string

我想创建一个类似于下图的函数。允许用户从设备 select 图像/pdf 文件并将文件转换为字符串形式的输入流并发送到服务器。
我经历了这个 Attaching a file of any type in Android application? 并成功地要求提供文件,但没有相机选项。感谢是否有任何我可以参考的资源或能够执行此操作的库:

  • provide option to select from gallery/take new photo/document
  • Select 文件并转换为字符串(输入流)

    //Json that server going to receive
    "Attachment": {
    "InputStream": "string",
    "FileName": "string"
    }
    
    
    /*My Code*/
    public void goToAttachment(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*|application/pdf");
        startActivityForResult(Intent.createChooser(intent, null), SELECTFILE_RESULT_CODE);
    
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        switch (requestCode){
            case SELECTFILE_RESULT_CODE:
                if(resultCode == RESULT_OK){
                    fileSrc = data.getData().getPath();
    
                }
                break;
        }
    }
    
  • 您可以使用以下代码首先获取 Bitmap 图像,然后从 Bitmap 对象获取 String

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        switch (requestCode){
            case SELECTFILE_RESULT_CODE:
                if(resultCode == RESULT_OK){
              // Let's read picked image data - its URI
            Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
            String bmpString= bitMapToString(bitmap)
            cursor.close();
            }
            break;
        }
    }
    

    您可以使用以下方法将 Bitmap 转换为 String:

    public String bitMapToString(Bitmap bitmap){
         ByteArrayOutputStream baos=new  ByteArrayOutputStream();
         bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
         byte [] b=baos.toByteArray();
         String temp=Base64.encodeToString(b, Base64.DEFAULT);
         return temp;
    }
    

    您可以将 Bitmap 压缩格式 JPGPNG 更改为您的图像。

    对于 PDF 和其他文件,您可以使用以下方法将其转换为字符串。

    private String getString(String filepath) throws IOException {
                InputStream inputStream = new FileInputStream(filepath);
                byte[] byteArray = IOUtils.toByteArray(inputStream);
                String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
                return encoded;
            }
    

    要获取文件路径,您可以使用以下代码:

    public String getRealPathFromURI(Context context, Uri contentUri) {
      Cursor cursor = null;
      try { 
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
      } finally {
        if (cursor != null) {
          cursor.close();
        }
      }
    }