RGM-79FP GM Striker RGM-79FP GM Striker - 2 months ago 8
Android Question

Uploading an image to parse after taking a photo

So I'm currently trying to create a button that starts the camera app and then takes the image and uploads it to the parse server, however it seems to fail uploading the image correctly, it crashes but it doesn't give me an error output, not sure why its possibly its trying to upload the file as an image. My code is below. ` private File imageFile;
TextView imageFilePath;
EditText editTxt;

String selectedVenue;
String id;
String currentUser;
ParseGeoPoint location;
String venueType;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gen_post);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
editTxt = (EditText) findViewById(R.id.txtInput);

Intent i = getIntent();
location = new ParseGeoPoint(i.getDoubleExtra("Lat",0.0),i.getDoubleExtra("Lon",0.0));
currentUser= ParseUser.getCurrentUser().getUsername();

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}

public void uploadComment(View view){
String input = editTxt.getText().toString();
Log.i("AppInfo", "uploading");
if(input.equals("")|| input.equals(null)){
Toast.makeText(this,"You must write something",Toast.LENGTH_SHORT);
}
else{
ParseObject object = new ParseObject("UserCommentary");
if(imageFile != null){
Bitmap bitmapImage = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 50, oStream);
byte[] byteArray = oStream.toByteArray();
object.put("comment", input);
object.put("Location",location);
object.put("image",true);
object.put("imgFile",byteArray);
}
else {
object.put("comment", input);
object.put("Location",location);
object.put("image",false);
}
object.saveInBackground();
}
onBackPressed();


}

public void takePhoto(View view) {

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), currentUser+".jpeg");
Uri tempuri = Uri.fromFile(imageFile);
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
takePicture.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
startActivityForResult(takePicture, 0);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0){
switch (resultCode){
case Activity.RESULT_OK:
if(imageFile.exists()){
imageFilePath.setText(imageFile.getAbsolutePath());

}
else{
Toast.makeText(this,"Error taking picture",Toast.LENGTH_LONG).show();
}
break;
case Activity.RESULT_CANCELED:
break;
default :
break;
}
}
}`

Answer

Here is a tutorial for how to upload an image on Parse cloud.

http://www.androidbegin.com/tutorial/android-parse-com-image-upload-tutorial/

From comparison between your implementation and tutorial one's, i think you're missing following code :

// Create the ParseFile passing byte array (image)
ParseFile file = new ParseFile("androidbegin.png", image); 
// Upload the image into Parse Cloud
file.saveInBackground();

and then use this parse file to store in parse object like following :

object.put("imgFile",file);

Hope it helps.

Comments