-4

我想在我的应用程序中添加一个功能,用户可以在其中将文件(PDF 文件)从他们的手机上传到数据库,然后将此文件下载回应用程序并显示它。

我不知道如何开始这样做以及使用什么正确的代码。

我尝试使用此代码,

ParseObject pObject = new ParseObject("ExampleObject");
  pObject.put("myNumber", number);
  pObject.put("myString", name);
  pObject.saveInBackground(); // asynchronous, no callback

- 编辑 -

我尝试了这段代码,但是当我单击按钮时应用程序崩溃了:

public class Test extends Activity {

    Button btn;
    File PDFFile;
    ParseObject po;
    String userPDFFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

       po = new ParseObject("pdfFilesUser");
        btn = (Button) findViewById(R.id.button);
        PDFFile = new File("res/raw/test.pdf");

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            uploadPDFToParse(PDFFile, po, userPDFFile);
            }
        });
    }

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



    }
4

2 回答 2

0

我强烈建议您快速掌握 Parse Java 开发 wiki。

回答你的问题。你想使用:

byte[] data = "Working at Parse is great!".getBytes();
ParseFile file = new ParseFile("resume.txt", data);

file.saveInBackground();

首先声明您的文件等,然后使用它保存它。但再一次,首先阅读指南以更好地了解您使用的框架。

https://parseplatform.github.io/docs/android/guide/

于 2016-12-12T07:30:39.167 回答
0

file您可以通过手动上传REST API在此处查看此文档

可以试试这段代码:

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

有关更多详细信息,请查看

于 2016-12-12T07:38:08.553 回答