package com.example.imagegalleryupload; import java.io.ByteArrayOutputStream; import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.provider.MediaStore; import android.provider.MediaStore.Images; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { final static int REQUEST_CODE = 0; //keep track of camera capture intent final int CAMERA_CAPTURE = 3; private String selectedImagePath; String mUploadMessage = ""; ImageView img ; private Uri imageUri; Uri selectedImageUri; //keep track of cropping intent final int PIC_CROP = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.imageView1); } public void btn_Upload_onClick(View view) { // Intent chon hinh tu gallery. Intent imageGalleryIntent = new Intent(Intent.ACTION_GET_CONTENT); imageGalleryIntent.addCategory(Intent.CATEGORY_OPENABLE); imageGalleryIntent.setType("image/*"); // Intent chup hinh tu camera final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Intent chooserIntent = Intent.createChooser(imageGalleryIntent, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[] { captureIntent }); /* Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CODE); */ startActivityForResult(chooserIntent, REQUEST_CODE); } public String getRealPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); /* if (requestCode == REQUEST_CODE) { if (null == this.mUploadMessage) { return; } Uri result; if (resultCode != RESULT_OK) { result = null; } else { result = data == null ? this.imageUri : data.getData(); // retrieve from the private variable if the intent is null } this.mUploadMessage = result.getPath(); img.setImageURI(result); this.mUploadMessage = null; Toast.makeText(this, mUploadMessage, Toast.LENGTH_LONG) .show(); } */ Toast toast = Toast.makeText(this, "" + requestCode + " * " + resultCode, Toast.LENGTH_LONG); toast.show(); try { switch (requestCode) { case REQUEST_CODE: if (android.os.Build.VERSION.SDK_INT < 13 ) { selectedImageUri = data.getData(); // case: android 2.3 va chup hinh tu camera if (selectedImageUri == null) { final ContentResolver cr = getContentResolver(); final String[] p1 = new String[] { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN }; Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC"); if ( c1.moveToFirst() ) { String uristringpic = "content://media/external/images/media/" +c1.getInt(0); Uri newuri = Uri.parse(uristringpic); selectedImageUri = newuri; Log.i("TAG", "newuri "+newuri); img.setImageURI(newuri); performCrop(); return; } } } if (resultCode == Activity.RESULT_OK) { //user is returning from capturing an image using the camera if(requestCode == CAMERA_CAPTURE){ //get the Uri for the captured image selectedImageUri = data.getData(); //carry out the crop operation performCrop(); } //user is returning from cropping the image else if(requestCode == PIC_CROP){ //get the returned data Bundle extras = data.getExtras(); //get the cropped bitmap Bitmap thePic = extras.getParcelable("data"); //display the returned cropped image img.setImageBitmap(thePic); return; } // data gives you the image uri. Try to convert that to // bitmap //selectedImageUri = data.getData(); selectedImagePath = getRealPath(selectedImageUri); img.setImageURI(selectedImageUri); break; } else if (resultCode == Activity.RESULT_CANCELED) { // Log.e(TAG, "Selecting picture cancelled"); } break; case PIC_CROP: if (resultCode == Activity.RESULT_OK) { //get the returned data Bundle extras = data.getExtras(); //get the cropped bitmap Bitmap thePic = extras.getParcelable("data"); //display the returned cropped image Uri cropImage = getImageUri(getBaseContext(), thePic); selectedImagePath = getRealPath(cropImage); Toast.makeText(this, "" + selectedImagePath + " * " + resultCode, Toast.LENGTH_LONG).show(); img.setImageBitmap(thePic); } else if (resultCode == Activity.RESULT_CANCELED) { // Log.e(TAG, "Selecting picture cancelled"); } break; } } catch (Exception e) { Log.e("error 1", "Exception in onActivityResult : " + e.getMessage()); } } public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } private void performCrop(){ try { //call the standard crop action intent (the user device may not support it) Intent cropIntent = new Intent("com.android.camera.action.CROP"); //indicate image type and Uri cropIntent.setDataAndType(selectedImageUri, "image/*"); //set crop properties cropIntent.putExtra("crop", "true"); //indicate aspect of desired crop cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); //indicate output X and Y cropIntent.putExtra("outputX", 256); cropIntent.putExtra("outputY", 256); //retrieve data on return cropIntent.putExtra("return-data", true); //start the activity - we handle returning in onActivityResult startActivityForResult(cropIntent, PIC_CROP); } catch(ActivityNotFoundException anfe){ //display an error message String errorMessage = "Whoops - your device doesn't support the crop action!"; Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); toast.show(); } } }
Sunday, 9 June 2013
upload image from camera + gallery android
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment