无法将pdf或docx文件转换为android中的base64

问题描述 投票:-1回答:1

我试图首先从内存中选择PDF或docx文件然后编码Base64字符串并存储在mysql数据库中。当我点击上传按钮时,它不起作用。我认为编码有问题,但我无法解决这个问题。如何解决这个问题还是有办法解决这个问题?

以下是我的代码

public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == RESULT_OK) {
               Uri uri = data.getData();
               assert uri != null;
               String uriString = uri.toString();
               file = new File(uriString);
               filepath = file.getAbsolutePath();
               String displayName ;
               if(uriString.startsWith("content://"))
               {
                    Cursor cursor = null;
                    try {
                         cursor = this.getContentResolver().query(uri,null,null,null,null);
                         if(cursor!=null&&cursor.moveToFirst())
                         {
                              displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                              Log.d("name",displayName);
                         }
                    }finally {
                         assert cursor != null;
                         cursor.close();
                    }
               }else if(uriString.startsWith("file://"))
               {
                    displayName = file.getName();
                    Log.d("name",displayName);
               }
          }
     }

     public static String convertFileToByteArray(File f) {
          byte[] byteArray = null;
          try {
               InputStream inputStream = new FileInputStream(f);
               ByteArrayOutputStream bos = new ByteArrayOutputStream();
               byte[] b = new byte[1024 * 11];
               int bytesRead;

               while ((bytesRead = inputStream.read(b)) != -1) {
                    bos.write(b, 0, bytesRead);
               }

               byteArray = bos.toByteArray();

          } catch (IOException e) {
               e.printStackTrace();
          }
          return Base64.encodeToString(byteArray, Base64.NO_WRAP);
     }
android base64 encode
1个回答
0
投票

以这种方式缩进

  public void getDoc() {

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/pdf");
    startActivityForResult(intent, 1);


}



 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK && data != null) {


        Uri selectedImage = data.getData();


        try {

                InputStream iStream = getContentResolver().openInputStream(selectedImage);
                byte[] inputData = getBytes(iStream);

                long fileSizeInBytes = inputData.length;
                long fileSizeInKB = fileSizeInBytes / 1024;
                long fileSizeInMB = fileSizeInKB / 1024;

                    ParseFile resumes  = new ParseFile("image.pdf", 
                          inputData);

        } catch (IOException e) {
            e.printStackTrace();

        }

    }
}

 public byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }
    return byteBuffer.toByteArray();
}
© www.soinside.com 2019 - 2024. All rights reserved.