Michael Cabus
Michael Cabus

Reputation: 121

File not saving as ParseFile, Android

I am attempting to save a photo Parse. The photo is taking, and I can preview it, however, I am not seeing the file on the Parse backend.

Here is my code:

* returning image / video
         */
        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();

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

            return mediaFile;
        }

I am using this as a reference: https://www.parse.com/docs/android/guide#files I also realize I need to get the data in byte format. So I am attempting to do that, but may not be doing that correctly.

Upvotes: 0

Views: 332

Answers (1)

Robert Rowntree
Robert Rowntree

Reputation: 6289

sample below uses apache httpClient rather than parse.android.SDK.saveInBackground() ... but it may help you with mapping the files bytes

                FileInputStream fis = new FileInputStream(mfile); 
                FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory
                int sz = (int)fc.size();
                MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
                data2 = new byte[bb.remaining()];
                bb.get(data2);
                ByteArrayEntityHC4 reqEntity = new ByteArrayEntityHC4(data2);
                httpPost.setEntity(reqEntity);
                fis.close();

Upvotes: 1

Related Questions