使用getBytes()来保存ParseFile,Android

时间:2015-05-06 15:14:53

标签: android parse-platform

我正在尝试使用此引用:

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

了解如何获取相机拍摄的文件并将其保存到Parse。我有这段代码:

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

但我收到错误:无法解析getBytes。

在使用getBytes之前,是否需要将此文件转换为其他内容?它的格式是否错误?

2 个答案:

答案 0 :(得分:2)

文件没有方法getBytes(),你需要将它转换为字节数组,如何回答:File to byte[] in Java

答案 1 :(得分:0)

以下是我的解决方案,基于此: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);


    }