Last active
December 15, 2015 18:30
-
-
Save DDRBoxman/5304544 to your computer and use it in GitHub Desktop.
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
Intent pickIntent = new Intent(); | |
pickIntent.setType("image/*"); | |
pickIntent.setAction(Intent.ACTION_GET_CONTENT); | |
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
File f = null; | |
try { | |
f = Util.createImageFile(); | |
mCurrentPhotoPath = f.getAbsolutePath(); | |
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); | |
} catch (IOException e) { | |
Crouton.makeText(this, "Could not access device storage", Style.ALERT).show(); | |
} | |
String pickTitle = "Select or take a new Picture"; | |
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); | |
chooserIntent.putExtra | |
( | |
Intent.EXTRA_INITIAL_INTENTS, | |
new Intent[]{takePhotoIntent} | |
); | |
startActivityForResult(chooserIntent, CAPTURE_PHOTO); |
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
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (resultCode == Activity.RESULT_OK) { | |
switch (requestCode) { | |
case CAPTURE_PHOTO: | |
String imageFilePath = null; | |
if (data != null && data.getData() != null) { | |
imageFilePath = Util.getImagePathForResult(this, data); | |
} else { | |
if (mCurrentPhotoPath != null) { | |
Util.galleryAddPic(this, mCurrentPhotoPath); | |
imageFilePath = mCurrentPhotoPath; | |
} | |
} | |
if (imageFilePath != null) { | |
SharePhotoActivity_.intent(HomeActivity.this).mPhotoPath(imageFilePath).start(); | |
} else { | |
Crouton.makeText(this, "Failed to fetch image", Style.ALERT).show(); | |
} | |
break; | |
default: | |
super.onActivityResult(requestCode, resultCode, data); | |
break; | |
} | |
} | |
} |
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
public static boolean isIntentAvailable(Context context, String action) { | |
final PackageManager packageManager = context.getPackageManager(); | |
final Intent intent = new Intent(action); | |
List<ResolveInfo> list = | |
packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); | |
return list.size() > 0; | |
} | |
public static File createImageFile() throws IOException { | |
File photoDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); | |
// Create an image file name | |
String timeStamp = | |
new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); | |
String imageFileName = "IMG" + timeStamp + "_"; | |
return File.createTempFile( | |
imageFileName, | |
".jpg", | |
photoDir | |
); | |
} | |
public static void galleryAddPic(Context context, String path) { | |
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); | |
File f = new File(path); | |
Uri contentUri = Uri.fromFile(f); | |
mediaScanIntent.setData(contentUri); | |
context.sendBroadcast(mediaScanIntent); | |
} | |
public static String getImagePathForResult(Context context, Intent data) { | |
String imageFilePath = null; | |
//User had pick an image. | |
Cursor cursor = context.getContentResolver().query(data.getData(), new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); | |
cursor.moveToFirst(); | |
//Link to the image | |
imageFilePath = cursor.getString(0); | |
cursor.close(); | |
if (imageFilePath == null) { | |
try { | |
InputStream is = context.getContentResolver().openInputStream(data.getData()); | |
File temp = new File("/sdcard/temp.jpg"); | |
FileOutputStream fos = new FileOutputStream(temp); | |
IOUtils.copy(is, fos); | |
is.close(); | |
fos.close(); | |
imageFilePath = temp.getAbsolutePath(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return imageFilePath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment