Capturing image, saving file ... Android / Parse
I am trying to save an image file captured from mobile in Parse in an android app. I am using final File mediaFile. I think my problem is: I know I need to convert the file to a byte array ... I've tried different ways to do it. I can see the image in the preview ... but I cannot save it in Parse.
Thanks for any help.
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;
}
}
+3
source to share
2 answers
I tried this and it works for me ...!
currentUser = ParseUser.getCurrentUser();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
//here bitmap is your selected photo 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();
}
}
});
in your case, you are already a byte of data in data2, so ...
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();
}
}
});
+2
source to share