将Pdf文件路径转换为Base64字符串

时间:2018-01-04 07:44:50

标签: android pdf base64 document docx

我正在尝试将我的pdf文件转换为Base64字符串,如下所示

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.DEFAULT);
}

但是因为它返回了我的空指针异常。正如我对此链接的评价

https://stackoverflow.com/a/35866301/4608769

任何人都可以为我提供完美的解决方案。

这是我的错误日志

STACK_TRACE=java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65539, result=-1, data=Intent { dat=content://com.android.fileexplorer.myprovider/external_files/Download/android_tutorial.pdf flg=0x1 }} to activity {com.voicenetpulse.pbxanywhereapp/com.voicenetpulse.pbxanywhereapp.kotlin.welcome.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.deliverResults(ActivityThread.java:4146)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4189)
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:6205)
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)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at android.util.Base64.encode(Base64.java:494)
at android.util.Base64.encodeToString(Base64.java:456)
at com.voicenetpulse.pbxanywhereapp.fax.Fax_Fragment.convertFileToByteArray(Fax_Fragment.java:548)
at com.voicenetpulse.pbxanywhereapp.fax.Fax_Fragment.onActivityResult(Fax_Fragment.java:511)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:7128)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4142)
... 9 more
java.lang.NullPointerException: Attempt to get length of null array
at android.util.Base64.encode(Base64.java:494)
at android.util.Base64.encodeToString(Base64.java:456)
at com.voicenetpulse.pbxanywhereapp.fax.Fax_Fragment.convertFileToByteArray(Fax_Fragment.java:548)
at com.voicenetpulse.pbxanywhereapp.fax.Fax_Fragment.onActivityResult(Fax_Fragment.java:511)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:7128)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4142)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4189)
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:6205)
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)

0 个答案:

没有答案