0

I am attempting to use this reference:

https://www.parse.com/docs/android/guide#files

To understand how to get a file taken by the camera and save it to Parse. I have this code:

private File getOutputMediaFile(int type) {

            // External sdcard location
            String appName = CameraActivity.this.getString(R.string.app_name);
            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    appName);

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(appName, "Oops! Failed create "
                            + appName + " directory");
                    return null;
                }
            }

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            final File mediaFile;
            if (type == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator
                        + "IMG_" + timeStamp + ".jpg");
            } else {
                return null;
            }




            final ParseFile photoFile;

            byte[] data = mediaFile.getBytes();

            // Save the scaled image to Parse
            photoFile = new ParseFile("profile_photo.jpg", data);

But I get the error: cannot resolve getBytes.

Do I need to convert this file to something else before using getBytes? Is it in the wrong format?

ScouseChris
  • 4,377
  • 32
  • 38
Michael Cabus
  • 121
  • 10

2 Answers2

2

File haven't method getBytes(), you need to convert it to byte array, how is answered here : File to byte[] in Java

Community
  • 1
  • 1
Nik Myers
  • 1,843
  • 19
  • 28
0

Here is my solution, based on this: http://www.mkyong.com/java/how-to-convert-file-into-an-array-of-bytes/:

private File getOutputMediaFile(int type) {

            // External sdcard location
            String appName = CameraActivity.this.getString(R.string.app_name);
            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    appName);

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(appName, "Oops! Failed create "
                            + appName + " directory");
                    return null;
                }
            }

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            final File mediaFile;
            if (type == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator
                        + "IMG_" + timeStamp + ".jpg");
            } else {
                return null;
            }




            final ParseFile photoFile;

            FileInputStream fileInputStream=null;

            byte[] bFile = new byte[(int) mediaFile.length()];

            try {
                //convert file into array of bytes
                fileInputStream = new FileInputStream(mediaFile);
                fileInputStream.read(bFile);
                fileInputStream.close();

                for (int i = 0; i < bFile.length; i++) {
                    System.out.print((char)bFile[i]);
                }

                System.out.println("Done");
            }catch(Exception e) {
                e.printStackTrace();

            }


            // Save the image to Parse
            photoFile = new ParseFile("profile_photo.jpg", bFile);
            photoFile.saveInBackground(new SaveCallback() {

                public void done(ParseException e) {
                    if (e != null) {


                    } else {
                        addPhotoToProfile(photoFile);

                    }
                }
            });

            return mediaFile;
        }

    private void addPhotoToProfile(ParseFile photoFile) {

        mCurrentUser.getCurrentUser();
        mCurrentUser.put("ProfilePhoto", photoFile);


    }
Michael Cabus
  • 121
  • 10