如何从pdf中选择base 64字符串在库中选择

时间:2018-05-24 10:15:13

标签: android

我通过传递mime类型" application / pdf"从图库中选择pdf。选择pdf后,我无法在onActivityResult中获取正确的文件数据。我想将从手机中选择的pdf文件编码为64字符串。

这是我的代码: -

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

   if (requestCode == 7 && resultCode == RESULT_OK && null != data) {
            Uri uri = data.getData();
            String uriString = uri.toString();
            File myFile = new File(uriString);
            base64String = convertFileToByteArray(myFile);

            if (base64String != null) {
                Log.d("Assignment","Base64 String : --> "+base64String );
            }
     }
 }

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 = 0;

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

        byteArray = bos.toByteArray();

        Log.e("Byte array", ">" + byteArray);

    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}

错误日志: -

   W/System.err: java.io.FileNotFoundException: content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D5693 (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at com.aeondigital.myfootprints.Teacher.Assignment.AssignmentEditCreateAcitivity.convertFileToByteArray(AssignmentEditCreateAcitivity.java:1152)
    at com.aeondigital.myfootprints.Teacher.Assignment.AssignmentEditCreateAcitivity.onActivityResult(AssignmentEditCreateAcitivity.java:1106)
    at android.app.Activity.dispatchActivityResult(Activity.java:7128)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4158)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4205)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6221)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

1 个答案:

答案 0 :(得分:1)

  

文件myFile = new File(uriString);

您不能使用File类,因为您有一个内容方案:

  

java.io.FileNotFoundException:content:/com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D5693(没有这样的文件或目录)       在java.io.FileInputStream.open(本机方法)

您应该只打开一个InputStream,而不是打开FileInputStream。

InputStream is = getContentResolver().openInputStream(data.getData());

此外,您不需要任何许可。