Android gallery not updated after ACTION_IMAGE_CAPTURE - java

I'm using MediaStore.ACTION_IMAGE_CAPTURE to take a picture with MediaStore.EXTRA_OUTPUT to point to the Uri for the DCIM/Camera directory where all the other photo/video files are stored.
The photo file is successfully taken and I can see it using ES File Explorer and can view it inside my app. However it is not shown in the gallery when I use Intent.ACTION_PICK
Intent selectPictureIntent = new Intent(Intent.ACTION_PICK);
selectPictureIntent.setType("image/*");
I've read the other topics on updating the Gallery after the picture comes back using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
and also
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
What's going on here :(

why did you use LocalBroadcastManager? LocalBroadcastManager can only send broadcast data inside of your app. Try to use
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
sendBroadcast(intent);
Gallery always listens to URI change. After file event broadcast to Gallery, the data stored in Gallery will be updated.

it also can problem with KITKAT build version.. for sure you can use this code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(out); \\out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}

Related

Can't save photo. Camera doesn't have permission to save to this location [Only happening to Google Pixel phones]

I am currently facing an issue with Google pixel phones not letting the app save photo to a specific file location (mentioned below). I don't know if this is something to do with the scoped-storage because the app is running perfectly fine with 4000+ android 12 devices and this only happens to google pixel devices.
(content://com.au.APP_NAME/data/user/0/com.au.APP_NAME/cache/APP_NAMEPicture-Cache/1660.jpg)
private void startCameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String filePath = FileProvider.contentUri + StorageManager.createPictureCacheFileName(getContext());
//filePath = content://com.au.APP_NAME/data/user/0/com.au.APP_NAME/cache/APP_NAMEPicture-Cache/1660.jpg
StorageManager.saveCameraCaptureImageFilePathToSharedPreferences(getContext(), filePath);
Uri fileURI = Uri.parse(filePath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileURI);
startActivityForResult(intent, Constant.TAKE_PHOTO_FROM_CAMERA_ACTIVITY_TAG);
}

How to create a share button for whatsapp, facebook or others app installed in my phone

I am developing an app, where I want to share the TEXT and IMAGE to other apps installed on my phone.
But when I click the share button Neither it shares TEXT nor IMAGE just empty it directs me to another app I chose for sharing.
below postDescription and postImage are my methods of model class, and I checked that am I getting values are not in a toast it gives there values properly.
Below is the code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, postDescription);
Uri uri = Uri.parse(postImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(shareIntent, "Share With"));
So the above bunch of code was not working, then I found code through which I can share my post TEXT and IMAGE to WhatsApp only, I tried that but it shows file formate not supported inside WhatsApp.
below is the code for sharing to WhatsApp only:
Uri imgUri = Uri.parse(postImage);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
I want to know how to create a share button that works fine for WhatsApp and everything or only WhatsApp will do the work for me.
By removing the package from the intent, Android should display the list of apps that can handle the type of intent.
Uri imgUri = Uri.parse(postImage); //Provide the URI to the downloaded image, not an external URL
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("*/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
if (whatsappIntent.resolveActivity(packageManager) != null) {
startActivity(whatsappIntent)
}

Sharing image from android app

I am trying to share an image from my Android app. I am trying to send it as an Email attachment as well as a photo on WhatsApp.
The code is:
String imageUrl = Path to image (eg. sdcard/pictures/image1.jpg);
shareImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uriToImage= Uri.parse(imageUrl);
Log.d("Image", ""+uriToImage);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image:"));
}
});
What's happening is:
On WhatsApp I can share the image easily.
On Gmail, it says that the attachment could not be sent.
On Hangouts I get a toast which says Photo couldn't be found
On Facebook too, the post is not accompanied with the Image but I can post.
On Facebook Messenger, it crashes without opening.
The tutorial that I have followed for this is given here. The send binary content part of the tutorial is the one that I have implemented.
Another thing I tried was to set the image in an ImageView and see if it is displayed. The image is displayed correctly. Also, the log message prints the correct path of the image.
I also read and tried the answers to: Question 1 and Question 2 but to no avail.
Where am I going wrong?
try this,
try
{
File myFile = new File(share_image_path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Yes the #CommonsWare Answer is correct you missed the Schema in your Intent.putExtra() and that's why it is failing to read your image in other social media platform
Here's my solution
Uri fileUri = Uri.fromFile(new File(imagePath));
//No need to do mimeType work or ext
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image:"));
BTW it works on all the mentioned platform
Uri image_uri = Uri.parse("file://"+imagePath);
Another method of creating Uri and passing it to intent

open Instagram hashtag intent

For opening an specific image in Instagram i use this intent:
Uri uri = Uri.parse("http://instagram.com/p/IMAGE_ID");
Intent insta = new Intent(Intent.ACTION_VIEW, uri);
if i want to see an specific hashtag all images what i have to do? Actually i want to open Instagram and see images of an hashtag
Uri uri = Uri.parse("https://instagram.com/explore/tags/your_tag/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

How to make a button that functions as a link in eclipse for android

I need to know how I could make a button that opens the androids web browser and navigates to a specific URL. I'm using eclipse and already know how to make buttons and click listeners.
Thanks for any help.
Just add this code to onClick():
// Launch a browser
Uri uri = Uri.parse("http://www.yahoo.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.android.com");
websiteIntent.setData(uri);
startActivity(websiteIntent);
Try this
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com");
startActivity(intent);

Categories

Resources