How to upload an image in parse server using parse api in android

pujitav picture pujitav · Apr 30, 2013 · Viewed 20.3k times · Source

I want to upload an image in parse cloud server in android. But I am unable to do so.

I have tried the following code:

    Drawable drawable = getResources().getDrawable(R.drawable.profilepic) ;
    Bitmap bitmap = (Bitmap)(Bitmap)drawable()
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] data = stream.toByteArray();                

    ParseFile imageFile = new ParseFile("image.png", data);
    imageFile.saveInBackground();

Please let me know how can I do it.


I've added a bounty to find the best definitive code for this common problem

Answer

Namit picture Namit · May 29, 2014

After struggling for several hours here is code segment works for me.

1. Data Member of activity class

Bitmap bmp;
Intent i;
Uri BmpFileName = null;

2. Starting the camera. Goal is to start camera activity and BmpFileName to store the referrence to file

String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {

String path = Environment.getExternalStorageDirectory().getName() + File.separatorChar + "Android/data/" + this.getPackageName() + "/files/" + "Doc1" + ".jpg";

File photoFile = new File(path);
try {
if (photoFile.exists() == false) { 
photoFile.getParentFile().mkdirs();
photoFile.createNewFile();
}
} 
catch (IOException e) 
{
Log.e("DocumentActivity", "Could not create file.", e);
}
Log.i("DocumentActivity", path);
BmpFileName = Uri.fromFile(photoFile);
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, BmpFileName);
startActivityForResult(i, 0);

3. Reading contents from Camera output by overriding onActivityResult. Goal is to get bmp variable valuated.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
bmp = MediaStore.Images.Media.getBitmap( this.getContentResolver(), BmpFileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
// Myocode to display image on UI - You can ignore
if (bmp != null)
IV.setImageBitmap(bmp);
}
}

4. On Save event

// MUST ENSURE THAT YOU INITIALIZE PARSE
Parse.initialize(mContext, "Key1", "Key2");

ParseObject pObj = null;
ParseFile pFile = null ;
pObj = new ParseObject ("Document");
pObj.put("Notes", "Some Value");

// Ensure bmp has value
if (bmp == null || BmpFileName == null) {
Log.d ("Error" , "Problem with image"
return;
}

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, stream);
pFile = new ParseFile("DocImage.jpg", stream.toByteArray());
try 
{
pFile.save();
pObj.put("FileName", pFile);
pObj.save();
_mParse.DisplayMessage("Image Saved");
} 
catch (ParseException e) 
{
// TODO Auto-generated catch block
_mParse.DisplayMessage("Error in saving image");
e.printStackTrace();
}

// Finish activity in my case . you may choose some thing else finish();

So here are key difference from others

  • I called initialize parse. You may laugh about it but folks have spent hour's debugging code without realize that parse was not initialized
  • Use Save instead of SaveInBackground. I understand that it may hold the activity but that is desired behavior for me and more importantly it works

Let me know if it does not work