I am trying to upload photo from camera, gallery and google photos. without camera code gallery and google photos is working properly but when I am adding the code for camera Intent is not returning the result as -1 and gallery and google photos is also not working properly :: it is copying the image to another folder but need to exit the app to show the image again in imageview. here is my code
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Picture");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getImageUri();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// Intent cameraIntent = getCameraIntent();
startActivityForResult(cameraIntent, 2);
}
else if (options[item].equals("Choose from Gallery"))
{
if (Build.VERSION.SDK_INT <= 19) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
} else if (Build.VERSION.SDK_INT > 19) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
here is code for activity result
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
String selectedImagePath=null;
if (requestCode == PICK_IMAGE_REQUEST && resultCode == getActivity().RESULT_OK && null != data) {
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
decodeFile(selectedImagePath);
}else if(requestCode==2&&resultCode==getActivity().RESULT_OK &&data!=null){
Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
decodeFile(selectedImagePath);
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Log.d("uri",selectedImagePath+":"+selectedImageUri);
}
}
code to copy the image
try {
File sd = Environment.getExternalStorageDirectory();
File directory = Environment.getExternalStorageDirectory();
String destinationPath = Environment.getExternalStorageDirectory().toString() + "/IASFolders/"+Configuration.empcode+".jpg";
if (sd.canWrite()) {
File source= finalFile;
File destination= new File(destinationPath);
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}else {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
after dst.close() it is getting out but not copying anything