【问题标题】:How do I upload a PDF file to Parse server?如何将 PDF 文件上传到 Parse 服务器?
【发布时间】:2020-01-30 18:39:05
【问题描述】:

我需要帮助在 android Studio 中使用 JAVA 将 pdf 文件上传到解析服务器。

我尝试使用以下代码:

private ParseObject uploadPDFToParse(File PDFFile, ParseObject po, String columnName){

    if(PDFFile != null){
        Log.d("EB", "PDFFile is not NULL: " + PDFFile.toString());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(PDFFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int read;
        byte[] buff = new byte[1024];
        try {
            while ((read = in.read(buff)) > 0)
            {
                out.write(buff, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] pdfBytes = out.toByteArray();

        // Create the ParseFile
        ParseFile file = new ParseFile(PDFFile.getName() , pdfBytes);
        po.put(columnName, file);

        // Upload the file into Parse Cloud
        file.saveInBackground();
        po.saveInBackground();
    }
    return po;
}

我收到此错误:

E/AndroidRuntime: 致命异常: main 进程:com.jackady.notebytebylev3l,PID:31048 java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/CourseRegistrationReport (1).pdf flg=0x1 }} 到活动 {com.jackady.notebytebylev3l/com.jackady.notebytebylev3l.uploadPDF}: java.lang.NullPointerException:尝试调用虚拟方法'int 空对象引用上的 java.io.BufferedInputStream.read(byte[])' 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4419) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4461) 在 android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:201) 在 android.app.ActivityThread.main(ActivityThread.java:6806) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 引起:java.lang.NullPointerException:尝试调用虚拟方法“int java.io.BufferedInputStream.read(byte[])” 空对象引用 在 com.jackady.notebytebylev3l.uploadPDF.uploadPDFToParse(uploadPDF.java:43) 在 com.jackady.notebytebylev3l.uploadPDF.onActivityResult(uploadPDF.java:140) 在 android.app.Activity.dispatchActivityResult(Activity.java:7590) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4412) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4461) 在 android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:201) 在 android.app.ActivityThread.main(ActivityThread.java:6806) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

【问题讨论】:

  • 你的PDFFile对象的类是什么?
  • PDFFile 是我使用从 uri.getpath() 获得的路径声明的文件类型

标签: java android server uri parse-server


【解决方案1】:

试试下面的代码,让我知道:

private ParseObject uploadPDFToParse(File PDFFile, ParseObject po, String columnName){

    if(PDFFile != null){
        Log.d("EB", "PDFFile is not NULL: " + PDFFile.toString());
        int size = (int) file.length();
        byte[] pdfBytes = new byte[size];

        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
            buf.read(pdfBytes, 0, pdfBytes.length);
            buf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Create the ParseFile
        ParseFile file = new ParseFile(PDFFile.getName() , pdfBytes);
        po.put(columnName, file);

        // Upload the file into Parse Cloud
        file.save();
        po.saveInBackground();
    }
    return po;
}

【讨论】:

    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    相关资源
    最近更新 更多