I am creating a application in which i want to send image to the web Server.For that i am using camera and gallery intent.Now what i want is to get the path of the image which user selects so that i can upload to the server.Now how can i get the path???
Code
if (userSelection.equals("Gallery")) {
intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
btChooseDoc.setText("Choose File");
}
if (userSelection.equals("Camera")) {
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
btChooseDoc.setText("Choose File");
}
if (userSelection.equals("Other")) {
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, 2);
btChooseDoc.setText("Choose File");
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Bitmap camera;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
if (requestCode == 1 && resultCode == getActivity().RESULT_OK) {
camera = (Bitmap) data.getExtras().get("data");
camera.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
galleryImage = Bitmap.createScaledBitmap(galleryImage, 150, 150, false);
image.setImageBitmap(camera);
}
if (requestCode == 0 && resultCode == getActivity().RESULT_OK) {
galleryUri = data.getData();
try {
galleryImage = decodeUri(getActivity(), galleryUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
galleryImage.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
galleryImage = Bitmap.createScaledBitmap(galleryImage, 150, 150, false);
image.setImageBitmap(galleryImage);
}
}
See Question&Answers more detail:os