图像捕获、保存文件…Android/Parse

图像捕获、保存文件…Android/Parse,android,parse-platform,Android,Parse Platform,我正试图在Android应用程序中保存从手机捕获的图像文件以进行解析。我使用的是最终文件mediaFile。我想我的问题是,我知道我需要将文件转换为字节数组…我尝试了各种方法来实现这一点。我可以在预览中看到图像…但我似乎无法保存它以进行解析 谢谢你的帮助 public Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } /**

我正试图在Android应用程序中保存从手机捕获的图像文件以进行解析。我使用的是最终文件mediaFile。我想我的问题是,我知道我需要将文件转换为字节数组…我尝试了各种方法来实现这一点。我可以在预览中看到图像…但我似乎无法保存它以进行解析

谢谢你的帮助

 public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));

    }


    /**
     * 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
                byte[] data2;

                FileInputStream fis = new FileInputStream(mediaFile);
                FileChannel fc = fis.getChannel();
                int sz = (int)fc.size();
                MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
                data2 = new byte[bb.remaining()];
                bb.get(data2);
                // Save the image to Parse
                photoFile = new ParseFile("profile_photo.jpg", data2);
                photoFile.saveInBackground();
                mCurrentUser.getCurrentUser();
                mCurrentUser.put("ProfilePhoto", photoFile);
                mCurrentUser.saveInBackground();

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

            }

            return mediaFile;
        }


    }

我已经尝试了这个,它的工作为我

currentUser = ParseUser.getCurrentUser();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
//here bitmap is your selected photo's bitmap    
bitmapProfilePic.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
final ParseFile file = new ParseFile("Profile.png", image);
file.saveInBackground(new SaveCallback() {
      @Override
      public void done(ParseException e) {
            if (e == null) {

            currentUser.put("ProfilePhoto", file);
            currentUser.saveInBackground(new SaveCallback() {
                     @Override
                     public void done(ParseException e) {
                          if (e == null) {
                                    Toast.makeText(context, "Updated..!!", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                        Toast.makeText(context, "Picture Updated..!!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(context, "Error to " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            });
在您的情况下,您已经在data2中有字节数据,因此

final ParseFile file = new ParseFile("YourPhotoName.jpg", data2);
file.saveInBackground(new SaveCallback() {
      @Override
      public void done(ParseException e) {
            if (e == null) {

            currentUser.put("ProfilePhoto", file);
            currentUser.saveInBackground(new SaveCallback() {
                     @Override
                     public void done(ParseException e) {
                          if (e == null) {
                                    Toast.makeText(context, "Updated..!!", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                        Toast.makeText(context, "Picture Updated..!!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(context, "Error to " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            });

您可以尝试压缩为JPEG格式,因为JPEG压缩会导致图像大小低于PNG(用于摄影图像)

请尝试以下代码:

bitmapProfilePic.compress(Bitmap.CompressFormat.JPEG, 100, stream);

谢谢…所以在我的代码中,mediaFile变量位于代码中bitmapProfilePic的位置?在我的代码中,它是在这里创建的:mediaFile=new File(mediaStorageDir.getPath()+File.separator+“IMG_”+timeStamp+”.jpg”);我的代码中没有bitmapProfilePic是所选图像的位图。所以你必须把它转换成位图来使用这段代码。但是你有byte[]数据,所以你可以直接使用它。啊,所以我有byte[]bFile=new byte[(int)mediaFile.length();。。所以我会放:bFile.compress(Bitmap.CompressFormat.PNG,100,stream);?不,不,你已经在数据2中有字节数据了,所以你只需要把它放在解析中。我已经编辑了我的答案。