0

I opened the music player to select an audio file using this code

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent_upload = new Intent();
            intent_upload.setType("audio/*");
            intent_upload.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent_upload,1);

        }
    });    

I called the uploadAudioToParse method inside OnActivityResult()

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){

    if(requestCode == 1){

        if(resultCode == RESULT_OK){

            //the selected audio.
            Uri uri = data.getData();
            File abc=new File(uri.toString());

            ParseObject ob=new ParseObject("songs");
            uploadAudioToParse(abc,ob,"song");
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

And this is my uploadAudioToParse method.

private ParseObject uploadAudioToParse(File audioFile, ParseObject po, String columnName){
    if(audioFile != null){
        Log.d("EB", "audioFile is not NULL: " + audioFile.toString());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(audioFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int read;
        byte[] buff = new byte[1024];
        try {
            assert in != null;
            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[] audioBytes = out.toByteArray();

        // Create the ParseFile
        ParseFile file = new ParseFile(audioFile.getName() , audioBytes);
        po.put(columnName, file);

        // Upload the file into Parse Cloud
        file.saveInBackground();
        po.saveInBackground();
    }
    return po;
}

is the file conversion method correct?

  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Shawn Mehan Oct 24 '15 at 04:22
  • I'm not sure about how to store the uri into a File. can someone tell me how to do it. Thanks:) – nigdeesh rajan Oct 26 '15 at 05:30

0 Answers0