I have this code:
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), CAMERA_IMAGE);
That allows that user to take a photo. Now how would I get the Uri of that photo in onActivityResult? Is it an Intent extra? Is it through Intent.getData()?
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
Uri u = intent.getData();
}
By the way... there's a bug with that intent in some devices. Take a look at this answer to know how to workaround it.
Instead of just launching the intent, also make sure to tell the intent where you want the photo.
Uri uri = Uri.parse("file://somewhere_that_you_choose");
Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(photoIntent, CAMERA_IMAGE);
Then when you get your onActivityResult() method called, if it was a success just open a stream to the URI and it should all be set.
Uri uri = null;
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
uri = data.getData();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
Uri fileUri = Utils.getUri(getActivity(), photo);
}
}
public String getRealPathFromURI (Uri contentUri) {
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}
Related
I'm trying to save a photo that I have already captured in my application (to use it later like as an icon for my contact) but I don't know if I must change it from uri to bitmap or any other type. I would like to save it as a .png like my others photos
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1020);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1020 && resultCode == RESULT_OK) {
mImageUri = data.getData();
Uri mImageUri2 = mImageUri;
Uri aux1=mImageUri;
Bitmap foto = photoUtils.getImage(aux1);
Drawable fotofinal =new BitmapDrawable(getResources(),foto);
Thanks for the help.
I know that probably this question is already here but i didnt find anything that could help me. I wanted to take a photo and save its path. I'm already taking the photo but i cant show the path in the Toast or save it in the database.
private static final int TAKE_PICTURE = 1;
private Uri outputFileUri;
SQLiteDatabase mydb;
static Uri capturedImageUri = null;
ImageView ecran;
Button b2,vertudo;
private String path;
ArrayList data;
ListView lista;
public void onClick(View v) {
try{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}catch(Exception e){
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
try{
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PICTURE) {
outputFileUri = data.getData();
path = getPath(outputFileUri);
mydb.execSQL("INSERT INTO caminho(nome) VALUES('"+path+"');");
Toast.makeText(getApplicationContext(), "Sucesso " + path,Toast.LENGTH_LONG).show();
}
}
}catch(Exception e){
Toast.makeText(getApplicationContext(),"Nao",Toast.LENGTH_LONG).show();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor =getContentResolver().query(uri, projection, null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
What you are trying to do in
path = getPath(outputFileUri);
I think data.getData() in onActivityResult wil directly returns the path of the captured image.
I would like to take a Photo that was Caputred with my App and send it via SFTP. I'm putting the Photo to an Specific folder:
timeStamp = new SimpleDateFormat("yyyyMMDD_HHmmss").format(new Date());
root = new File(Environment.getExternalStorageDirectory()+ File.separator + "OpenClinica" + File.separator);
root.mkdirs();
sdDir = new File(root, "OC_" + timeStamp + ".jpg");
Now I need to take this picture by a click of a button and send it via SFTP.
I have the classes/methods for SFTP, but I can not get the file selector.
Thank you for Helping
Try this code:
#Override
public void onClick(View v) {
if (v.getId() == findViewById(R.id.ID).getId()){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Seleccionar vídeo"), PICK_IMAGE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an video.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the video
final String imageFilePath = cursor.getString(0);
cursor.close();
}
}
Hope it´s useful!!
Thanks a lot is solved it this way:
protected void startCameraActivity() {
outputFileUri = Uri.fromFile(sdDir);
i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, 0);
}
//Manage everything that happens after the Camera was started
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//
// Write the Captured Image as File
Intent intent = new Intent();
intent.putExtra("uri", sdDir.getPath());
//Grab the Captured Image from the Cache an create the Preview
bmp = BitmapFactory.decodeFile(outputFileUri.getPath());
//Rotates the Preview Image
Matrix matrix=new Matrix();
matrix.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bmp, 0, 0,bmp.getWidth(),bmp.getHeight(), matrix, true);
//Set the Rotated Image as Preview in the ImageView from the Layout
iv.setImageBitmap(bMapRotate);
setResult(0, intent);
}
I'm having trouble using the camera when there's no sdcard present.
When there is an sdcard, using the camera is trivial, e.g.
http://www.vogella.com/articles/AndroidCamera/article.html + a plethora of other examples.
However, I need to make my app available to devices which don't have SD cards, (e.g. the Sony Xperia series.) I've tried modifying the code such that I'm using the internal storage (I think):
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getDir("myDirec", Context.MODE_WORLD_WRITEABLE), "tmp_photo_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
file.createNewFile();
mImageCaptureUri = Uri.fromFile(file);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
However, upon result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentReturn) {
if (resultCode != RESULT_OK)
return;
String path = mImageCaptureUri.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(path);
...
bitmap is null.
Which leads me to believe that there's some permissions issue....maybe?
I've tried some of the other internal storage options, http://developer.android.com/guide/topics/data/data-storage.html#filesInternal e.g. getFilesDir() but the same result: null bitmap.
Has anyone had any success in using the camera without an sdcard?
Try this. It works..
private Uri imageUri;
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnImageCapture:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File dir = context.getDir("directory", Context.MODE_PRIVATE);
File photo = new File(dir, "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, OPEN_CAMERA);
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case OPEN_CAMERA:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}
}
}
In the application the user can select an image. To start the gallery I use the following code:
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
When I print the path in onActivityResult method, I get the image path like /external/images/media/5615. I'd like to know the image extension, because the image needs to be uploaded to a server. The extension may vary.
What you're getting is a path the MediaStore uses to get the Images. The following code will take the result from your Intent, and get the filename attached to it:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
Log.i(TAG, "" + selectedImage.toString());
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.i(TAG, "" + picturePath);
//Do whatever with picturePath
}