-4

I want to add a feature to my app in which the users can upload files (PDF files) from their mobile to the database, then download this file back to the app and display it.

I have no idea how to start doing this and what is the right code to use.

I tried using this code,

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

- EDIT -

I tried this code but the app crashes when I click the button:

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 Answers2

0

I would strongly suggest you quickly get up to speed with the Parse Java development wiki.

To answer your question. You want to be using:

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

file.saveInBackground();

First declare your file etc then save it using that. But once again, first read the guidelines to better understand the framework you working with.

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

Cliffordwh
  • 1,422
  • 1
  • 10
  • 18
0

You can upload a file manually via REST API. Take a look at this docs here

Can try this 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;
}

For more details check this

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • Thank you very much for your answer. The link really helped me understand Parse objects and files. And I now know how to use them. But the thing I still do not understand is where am I supposed to store the pdf file to (I'm using android studio) before doing all of this? I mean what is inside this variable: "PDFFile"? How can I save the pdf file I have in my computer in it? – Deema Al-Shami Dec 12 '16 at 16:56
  • @DeemaAl-Shami "How can I save the pdf file I have in my computer in it?" -- You cannot. That PDF is on your computer, not on the Android Device where you are trying to upload it from – OneCricketeer Dec 12 '16 at 18:33
  • Then what is this code storing? Where is the pdf file it is storing? @cricket_007 – Deema Al-Shami Dec 12 '16 at 18:48
  • @DeemaAl-Shami All your question asked was "how to send PDF to Parse". The first parameter `File PDFFile` allows you to pass any file from your Android device's hard drive, which is entirely separate from your computer. – OneCricketeer Dec 12 '16 at 19:05
  • Aah yes, I get it now. Thank you very much ^^ – Deema Al-Shami Dec 12 '16 at 19:06
  • The thing is, it says that this method is not used. That is why, I'm asking how to call this method and what parameters to use while calling it when the button is clicked? Very sorry for too many questions >. – Deema Al-Shami Dec 12 '16 at 19:08
  • You are Welcome @DeemaAl-Shami For more clarification you can read the documentation over the website I have mentioned. – Maveňツ Dec 13 '16 at 04:36