The following code allows me to take pictures, however it does not insert the image into the db. What am I missing?
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(Media.TITLE, "Image");
values.put(Images.Media.BUCKET_ID, "292");
values.put(Images.Media.BUCKET_DISPLAY_NAME, "thename");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Media.DESCRIPTION, "Image capture by camera");
values.put("_data", IMG_FILE_PATH);
Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
i.putExtra("return-data", true);
startActivityForResult(i,REQ_PHOTO);
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(imageIntent, REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY);
Try just using this code. It seems like you overwriting _data is causing issues when Android tries to store the image.
Related
I want to program an app. In this app, the user selects an image file path and the app opens another app (Google Photos) to show that image. This image is in the public folder Downloads.
I am using Android 9.0.
What I tried:
File imageFile = new File("/storage/emulated/0/DCIM/Camera/IMG_20220203_114716.jpg");
Uri uri = Uri.fromFile(imageFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
And in AndroidManifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I get a FileUriExposedException.
SOLVED:
Uri collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
String[] projection = new String[]{MediaStore.Files.FileColumns._ID};
String selection = MediaStore.Files.FileColumns.DATA + "=?";
String[] selectionArgs = new String[]{"/storage/emulated/0/DCIM/Camera/IMG_20220203_114716.jpg"};
String sortOrder = null;
Cursor cursor = getApplicationContext().getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder);
cursor.moveToFirst();
Uri uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.apps.photos");
intent.setDataAndType(uri, "image/*");
startActivity(intent);
I first created a new pdf file using:
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DISPLAY_NAME, file_name);
values.put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf");
values.put(MediaStore.Files.FileColumns.RELATIVE_PATH, "Documents");
ContentResolver resolver = context.getContentResolver();
Uri uri = resolver.insert(MediaStore.Files.getContentUri("external"), values);
fos = resolver.openOutputStream(uri);
document.writeTo(fos);
fos.flush();
fos.close();
values.clear();
resolver.update(uri, values, null, null);
Now I pass this "uri" to different activity where I choose Intent.Action_Send to share pdf file to say whatsapp using:
Intent intentShareFile;
intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.setType("application/pdf");
startActivityForResult(Intent.createChooser(intentShareFile, "Share File"), FINISH_SHARING);
This open the intent chooser window with multiple apps, whatsapp, facebook, but shows the number "39913" from the uri "content://media/external/file/39913" instead of file name. I expect to show my file name here "New.pdf". Can you please solve the problem?
I am using camera intent for capturing selfie but by default it open back camera and I want front camera by default.
ContentValues values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 101);
Add the following line:
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1)
Although, it doesn't seem to always work. But it's the best you can get using just intents.
Is it possible to Capture images via camera in android by passing uri of internal storage in the Intent? If yes How? I tried in the following way but it gave JHEAD can't read error.
File mediaStorageDir = getFilesDir();
File newFile = new File(mediaStorageDir,IMAGE_DIRECTORY_NAME);
Uri newUri = Uri.fromFile(newFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
It is because of small mistake. You are passing directory rather file name.
Try this way hope this helps you
File mediaStorageDir = getFilesDir();
File newFile = new File(mediaStorageDir,new Date().getTime() + ".png");
Uri newUri = Uri.fromFile(newFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, newUri);
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
I have image editing app with image export function. Previously I tryed to export it directly by passing file path uri to ACTION_SEND intent. Something like:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.export)));
But not all apps can correctly parse such intent. So now I first prepare uri by adding image to android's MediaStore:
shareUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), projectName));
...and then passing shareUri to intent. Now all apps can correctly process this intent, but one problem appear - each time when user exports image its copy added to android MediaStore(even if image path and name the same).
I trying to determine is image already added to mediastore by following code:
Cursor c = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media.DATA + "=?",
new String[] { filePath }, null);
if (c.getCount() > 0 && c.moveToFirst()) {
shareUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"" + c.getInt(c.getColumnIndex(MediaStore.Images.Media._ID)));
c.close();
} else {
shareUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), projectName));
}
...but c.getCount() always == 0. Even if I copy saved uri from MediaStore.Images.Media.insertImage and query it directly by
Cursor c = getContentResolver().query(savedUri, null, null, null);
How I can detect is image file already in MediaStore?
Found solution: not using MediaStore.Images.Media.insertImage to add file to gallery but doing similar by MediaScannerConnection:
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
new String[]{"image/*"},
new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.export)));
}
});