I have a simple choose image source
dialog :
public void toggleImagePicker()
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[]{"Gallery", "Camera"}, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
// inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
switch (which) {
case 0:
// GET IMAGE FROM THE GALLERY
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent.createChooser(intent, "Choose a Picture");
if (mBackgroundClicked)
startActivityForResult(chooser, REQUEST_GALLERY_IMAGE_FOR_BACKGROUND);
else
startActivityForResult(chooser, REQUEST_GALLERY_IMAGE);
break;
case 1:
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(), ".helper/");
else
cameraFolder = getActivity().getCacheDir();
if (!cameraFolder.exists())
cameraFolder.mkdirs();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(), ".helper/" + imageFileName);
if (!mBackgroundClicked)
mProfilePhotoPath = new String(photo.getAbsolutePath());
else
mBackgroundPhotoPath = new String(photo.getAbsolutePath());
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
// initialURI = Uri.fromFile(photo);
if (mBackgroundClicked)
startActivityForResult(getCameraImage, REQUEST_IMAGE_CAPTURE_FOR_BACKGROUND);
else
startActivityForResult(getCameraImage, REQUEST_IMAGE_CAPTURE);
break;
default:
break;
}
}
});
builder.show();
}
And i try to pick image, i show the dialog and click on Camera or Gallery. Otherwise my activity is destroyed and recreated.
No exceptions in LogCat.
Single thing what i found in logs :
06-11 14:45:57.700 458-549/? W/InputDispatcher﹕ channel '221e47c0 ******.ProfileExpertActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
Note : pick mechanism implemented in fragment.
If there are no application to handle the action then it will results the NullPointerException
before starting the check with resolveActivity to check whether there are any application to handle it or not.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
// Verify that the intent will resolve to an activity
if (photoPickerIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(photoPickerIntent, GALLERY_PICKER);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.