Skip to content

Instantly share code, notes, and snippets.

@gvsharma
Created June 5, 2014 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvsharma/e06a5e18a2da99594eac to your computer and use it in GitHub Desktop.
Save gvsharma/e06a5e18a2da99594eac to your computer and use it in GitHub Desktop.
getting an image from camera or gallery and set it as
private Uri getTempFile(){
File root = new File(Environment.getExternalStorageDirectory(), "fashion_swap");
if (!root.exists()){
root.mkdirs();
}
String filename=""+System.currentTimeMillis();
File file = new File(root,filename+".jpeg" );
Uri muri = Uri.fromFile(file);
selectedImagePath = muri.getPath();
Log.v("take picture path",selectedImagePath);
myAccountInfo.setLongitude(longi);
myAccountInfo.setLattitude(lati);
return muri;
}
private void selectImage() {
final CharSequence[] options = { "Capture From Camera","Choose from Gallery", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(UserRegistrationActivity.this);
builder.setTitle("Upload Photo");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Capture From Camera")) {
if (isSDCARDMounted()) {
Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempfile=new File(Environment.getExternalStorageDirectory(),"sample.jpg");
Uri uritemp = Uri.fromFile(tempfile);
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
photoPickerIntent.putExtra("return-data", true);
myAccountInfo.setLongitude(longi);
myAccountInfo.setLattitude(lati);
startActivityForResult(photoPickerIntent, REQUEST_IMAGE_CAPTURE);
} else {
Toast.makeText(UserRegistrationActivity.this,"You need to insert SD card", Toast.LENGTH_LONG).show();
}
} else if (options[item].equals("Choose from Gallery")) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_IMAGE_GALLERY);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
if(REQUEST_IMAGE_GALLERY == requestCode){
Uri selectedImage = data.getData();
selectedImagePath = getPath(selectedImage);
myAccountInfo.setLongitude(longi);
myAccountInfo.setLattitude(lati);
Bitmap rotatedBitmap=null;
Bitmap bitmap=null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), selectedImage);
Matrix matrix = new Matrix();
matrix.postRotate(0);
bitmap = Bitmap.createScaledBitmap(bitmap,desiredImageWidth,desiredImageHeight,true);
rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap .getWidth(), bitmap .getHeight(), matrix, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.upload_img_icon);
}
Log.d("PATH", "PATH: "+ selectedImagePath);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// myAccountInfo.setLongitude(longi);
// myAccountInfo.setLattitude(lati);
Cursor cursor = getApplicationContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
cursor.close();
addpro_imv.setImageBitmap(rotatedBitmap);
isImageUploaded = true;
if(myAccountInfo.getUserId() !=null){
isChanged = true;
}
} else if(requestCode == REQUEST_IMAGE_CAPTURE){
int rotate = 0;
Log.d("PATH",""+selectedImagePath);
int index = selectedImagePath.lastIndexOf("/");
String filename = selectedImagePath.substring(index+1, selectedImagePath.length());
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/title_info/";
File file = new File(filepath,filename);
ExifInterface exif;
try {
exif = new ExifInterface(file.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (IOException e1) {
e1.printStackTrace();
}
FileInputStream fs = null;
try{
fs = new FileInputStream(file);
}
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "File "+e.getMessage(), Toast.LENGTH_LONG).show();
}
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inJustDecodeBounds = false;
bfOptions.inTempStorage = new byte[32 * 1024];
Bitmap bitmap = null;
Bitmap rotatedBitmap = null;
try {
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(selectedImagePath),desiredImageWidth,desiredImageHeight,false);
rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap .getWidth(), bitmap .getHeight(), matrix, true);
} catch (Exception e) {
e.printStackTrace();
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.upload_img_icon);
}
addpro_imv.setImageBitmap(rotatedBitmap);
isImageUploaded = true;
if(myAccountInfo.getUserId() != null){
isChanged = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment