将文件上传到Parse数据库

时间:2016-12-11 18:15:32

标签: java android parse-platform back4app

我想在我的应用中添加一项功能,用户可以在其中将文件(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;
    }



    }

2 个答案:

答案 0 :(得分:0)

我强烈建议您快速熟悉Parse Java开发维基。

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

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

file.saveInBackground();

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

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

答案 1 :(得分:0)

您可以通过file手动上传REST API。请查看此文档here

可以试试这段代码:

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

有关详细信息,请查看this