vendredi 20 février 2015

upload photo to parse ( android)

I'm a new parse.com user and i want to upload image to parse.com that chosen by the user.


the user choose the image , then the name of it and then press the upload button


but when i click the button upload the program is crash :(


this is my try



Button btn;
ImageView Pic;
ParseObject shop;
final int PHOTO_SELECTED = 1;
ParseFile file;
ParseFile photoFile;
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.openshop);

btn = (Button) findViewById(R.id.button1);
Pic = (ImageView) findViewById(R.id.imageView1);
final int PHOTO_SELECTED = 1;


Pic.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,PHOTO_SELECTED);

}
});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PHOTO_SELECTED && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
final String picturePath = cursor.getString(columnIndex);
cursor.close();

final Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
Pic.setImageBitmap(bitmap);


btn.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
public void onClick(View arg0) {
EditText name = (EditText) findViewById(R.id.sname);

shop = new ParseObject("imagetest");

// Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);


Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Pic);



// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();

// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile(name.getText().toString()+".png", image);

// Upload the image into Parse Cloud
file.saveInBackground();


//Create
shop.put("name", name.getText().toString());
shop.put("UserOpen",ParseUser.getCurrentUser());
shop.put("Image", file);

shop.saveInBackground();


// Show a simple toast message
Toast.makeText(getApplicationContext(), "Created",
Toast.LENGTH_SHORT).show();
}

});

}
}


}


thankyou


Aucun commentaire:

Enregistrer un commentaire