简体   繁体   中英

How do I upload a PDF file to Parse server?

I need help uploading pdf file to the parse server using JAVA in android Studio.

I tried using the following code:

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;
}

I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jackady.notebytebylev3l, PID: 31048 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/CourseRegistrationReport (1).pdf flg=0x1 }} to activity {com.jackady.notebytebylev3l/com.jackady.notebytebylev3l.uploadPDF}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.BufferedInputStream.read(byte[])' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4419) at android.app.ActivityThread .handleSendResult(ActivityThread.java:4461) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6806) at Z93F725A07423F E1C889F448B33D21F46Z.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.BufferedInputStream.read(byte[])' on a null object reference at com.jackady.notebytebylev3l.uploadPDF.uploadPDFToParse(uploadPDF.java:43) at com.jackady.notebytebylev3l.uploadPDF.onActivityResult(uploadPDF.java:140) at ZC31B32364CE19CA8FCD150A417ECCE5 8Z.app.Activity.dispatchActivityResult(Activity.java:7590) at android.app.ActivityThread.deliverResults(ActivityThread.java:4412) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4461) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) at android.os.Handler.dispatchMessage(Handl er.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6806) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Try the following code and let me know:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM