- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要帮助在 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: 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 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) 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 android.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(Handler.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)
最佳答案
尝试以下代码并告诉我:
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;
}
关于java - 如何将 PDF 文件上传到 Parse 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58185321/
我是一名优秀的程序员,十分优秀!