Created
August 27, 2014 20:10
-
-
Save mistrydarshan99/d74d550f0b81f47772ad to your computer and use it in GitHub Desktop.
choose image from camera and gallery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void selectImage() { | |
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; | |
AlertDialog.Builder builder = new AlertDialog.Builder(SignUp.this); | |
builder.setTitle("Add Photo!"); | |
builder.setItems(options, new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int item) { | |
if (options[item].equals("Take Photo")) | |
{ | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); | |
startActivityForResult(intent, 1); | |
} | |
else if (options[item].equals("Choose from Gallery")) | |
{ | |
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(intent, 2); | |
} | |
else if (options[item].equals("Cancel")) { | |
dialog.dismiss(); | |
} | |
} | |
}); | |
builder.show(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (resultCode == RESULT_OK) { | |
if (requestCode == 1) { | |
File f = new File(Environment.getExternalStorageDirectory().toString()); | |
for (File temp : f.listFiles()) { | |
if (temp.getName().equals("temp.jpg")) { | |
f = temp; | |
break; | |
} | |
} | |
try { | |
Bitmap bitmap; | |
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); | |
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), | |
bitmapOptions); | |
userImage.setImageBitmap(bitmap); | |
String path = android.os.Environment | |
.getExternalStorageDirectory() | |
+ File.separator | |
+ "Phoenix" + File.separator + "default"; | |
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP | |
Uri tempUri = getImageUri(getApplicationContext(), bitmap); | |
// CALL THIS METHOD TO GET THE ACTUAL PATH | |
// File finalFile = new File(getPath(tempUri)); | |
filePath1 = getPath(tempUri); | |
f.delete(); | |
OutputStream outFile = null; | |
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); | |
try { | |
outFile = new FileOutputStream(file); | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile); | |
outFile.flush(); | |
outFile.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else if (requestCode == 2) { | |
Uri selectedImage = data.getData(); | |
String[] filePath = { MediaStore.Images.Media.DATA }; | |
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); | |
c.moveToFirst(); | |
int columnIndex = c.getColumnIndex(filePath[0]); | |
String picturePath = c.getString(columnIndex); | |
filePath1 = picturePath; | |
c.close(); | |
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); | |
Log.w("path of image from gallery......******************.........", picturePath+""); | |
userImage.setImageBitmap(thumbnail); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what this part of the code is doing?