Camera API android 10 - java

Error showing while capturing image ,I already give the read write storage permission of device
I am using camera intent for taking photo in android 10, But shows following error
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentResolver resolver = getContext().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "temp");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + "TEMP");
Uri imageUri123 = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
imageUri123);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}else {
File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Build.VERSION.SDK_INT >= Build.VERSION_CODES.M?getOutputMediaFileUri(getActivity(), photo):Uri.fromFile(photo));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}

Related

Load image in bitmap from Uri in android Q

There are similar questions but with deprecated code for android Q...
I am taking and storing photos in a custom folder, for android Q I use:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = getActivity().getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, namePhoto() + ".jpg");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Folder");
imagenUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
MyPreferences.setPhotoUri(imagenUri.toString(), getContext());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imagenUri);
}
startActivityForResult(takePictureIntent, 199);
}
setPhotoUri stores this content://media/external/images/media/201,
I use the uri to show the photo:
imageView.setImageURI(Uri.parse(MyPreferences.getPhotoUri(getContext())));
the problem is that I need to show a lot of photos and I understand that setImageURI can consume a lot of memory and I can't load the image with Bitmap, I only want to show thumbnails, everything I find on the internet is deprected, how can i do it in Android Q?

How to share image through inten

Bundle intent = getIntent().getExtras();`
cardView=(CardView)findViewById(R.id.card);
final String query = intent.getString("Query1");
db = new DataBaseHelper(Image.this);
Cursor c = db.getData(query);
if (c.getCount() != 0) {
c.moveToFirst();
do {
image = c.getString(5);
title=c.getString(3);
} while (c.moveToNext());
}
txt.setText(title);
img.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(image, "drawable", getPackageName())));
To create an Share Intent for image, you can use following code.
uriToImage is Uri of your image file.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
EDIT - To get URI of drawable image
Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
Use Content Provider(FileProvider) To Share Data as it is Good Practice.
Intent intent = new Intent(Intent.ACTION_SEND);
final String path = "Your File Path";
Uri uri = FileProvider.getUriForFile(YourActivity.this, "com.abc.xy.appname.fileprovider", new File(path));
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
For Multiple File Sharing
ArrayList<Uri> files = new ArrayList<Uri>();
for (int i = 0; i < mfileList.size(); i++) {
File file = new File(mfileList.get(i).get_path());
Uri uri = FileProvider.getUriForFile(YourActivity.this, "com.abc.xy.appname.fileprovider", file);
files.add(uri);
}
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
To Share an Image Stored in Your Drawable.Put this Uri in your Share Intent Extras.
Uri uri = Uri.parse("android.resource://your.package/drawable/image_name");
Check this Link out For Content Provider Setup.
[https://developer.android.com/reference/android/support/v4/content/FileProvider][1]

FileUriExposed exception error

im getting the following error in my logcat.
: android.os.FileUriExposedException: file:///storage/emulated/0/download/AllCast.apk exposed beyond app through Intent.getData()
it only happening on some devices ie marsh mellow and nougat.
this is where error occurs
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
ive tried adding this line of code
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
also tried this in my app class
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
can anybody tell me what im doing wrong. thanks
EDIT****
Ok guys im trying to get #CommonsWare answer to work i can get the file to download fin to the provider folder. but then my app crashes when it trys to handle the intent. here is my new log cat
05-07 16:25:35.784 8715-8715/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: mypackagename, PID: 8715
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getName()' on a null object reference
at my package name.Casting_Apps$DownloadFileFromURL.onPostExecute(Casting_Apps.java:273)
which leads me to section of code
File newFile = null;
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(getBaseContext(), "my package name", newFile);
intent.setDataAndType(contentUri, type);
} else {
intent.setDataAndType(Uri.fromFile(newFile), type);
}
startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT);
} catch (ActivityNotFoundException anfe) {
Toast.makeText(getBaseContext(), "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
This is the answer i went for.. thanks #CommonsWare for the help
File toInstall = new File(appDirectory, appName + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent)
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}

Can't send image in my application to external app as implicit intent

I am new to android i have read mant aricles on this from stackoverflow and other resources but i tried my best to get result
At time of sending i select gmail to send my image but its showing empty file cant be attached
i tried with different images in my package but i get the same response
Here is my code which i tried
Intent intent;
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + getResources().getResourcePackageName(R.drawable.cursor)
+ '/' + getResources().getResourceTypeName(R.drawable.cursor) + '/' +
getResources().getResourceEntryName(R.drawable.cursor) );
intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
chooser=Intent.createChooser(intent, "Select app to send");
startActivity(chooser);
Hope this helps:
BitmapFactory.Options options = new BitmapFactory.Options();
options.outMimeType = "image/jpeg";
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher, options);
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Image"));

Deleting Inserted ContentValues

I have created custom intents for the user to choose photos or take a picture.
Here's my Code:
Intent pickIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "new image");
values.put(MediaStore.Images.Media.DESCRIPTION, "saved today");
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraPhotoUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPhotoUri);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[]{takePhotoIntent});
startActivityForResult(chooserIntent, REQUEST_CODE_ADD_PHOTO);
now the getContentResolver().insert(...) part creates an empty file on the image directory.How do I delete all of them (empty files) after using the intents. Please help.

Categories

Resources