I'm trying to upload a single image with profile creation in my app. I created a button to browse the image from gallery/camera, but the code shows error msg as 'Unreachable code'. I receive this error only for image browsing on button click. rest of the code works fine for form creation and save it on SQLite. Part of the code is given below. Plz help.
public class NewPetsFragment extends Fragment implements OnClickListener{
private DBCreater dbCreate;
//Button addImage;
private static final int CAMERA_REQUEST = 1;
private static final int PICK_FROM_GALLERY = 2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View gv = inflater.inflate(R.layout.new_pet, null);
Spinner sp = (Spinner) gv.findViewById(R.id.ETPetType);
// get reference
sp.setAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_spinner_dropdown_item, petType));
Button btnSubmit = (Button) gv.findViewById(R.id.ButtonNext);
btnSubmit.setOnClickListener(this);
return gv;
/**
* open dialog for choose camera/gallery
*/
final String[] option = new String[] { "Take from Camera",
"Select from Gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getBaseContext());
builder.setTitle("Select Option");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.e("Selected Item", String.valueOf(which));
if (which == 0) {
callCamera();
}
if (which == 1) {
callGallery();
}
}
});
final AlertDialog dialog = builder.create();
View pv = inflater.inflate(R.layout.new_pet, null);
Button addImage = (Button) pv.findViewById(R.id.ETPetImg);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
}
/**
* open camera method
*/
public void callCamera() {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("crop", "true");
cameraIntent.putExtra("aspectX", 0);
cameraIntent.putExtra("aspectY", 0);
cameraIntent.putExtra("outputX", 200);
cameraIntent.putExtra("outputY", 150);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
/**
* open gallery method
*/
public void callGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
Just return gv at the end
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View gv = inflater.inflate(R.layout.new_pet, null);
Spinner sp = (Spinner) gv.findViewById(R.id.ETPetType);
// get reference
sp.setAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_spinner_dropdown_item, petType));
Button btnSubmit = (Button) gv.findViewById(R.id.ButtonNext);
btnSubmit.setOnClickListener(this);
/**
* open dialog for choose camera/gallery
*/
final String[] option = new String[] { "Take from Camera",
"Select from Gallery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getBaseContext());
builder.setTitle("Select Option");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.e("Selected Item", String.valueOf(which));
if (which == 0) {
callCamera();
}
if (which == 1) {
callGallery();
}
}
});
final AlertDialog dialog = builder.create();
View pv = inflater.inflate(R.layout.new_pet, null);
Button addImage = (Button) pv.findViewById(R.id.ETPetImg);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
return gv;
}
But I recommend you to store images as app cache.