I'm currently having a problem with making my pictures contained in the app/files/Picture folder appear in the gallery with a device running Android 10. I created a folder in Picture called "ticket" (for sorting purposes) and I'm trying to display the entire folder in the gallery.
The following code works perfectly fine with a device running Android 9, but I just can't manage to make it work with the other device.
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File dir = new File(currentDirPath); // currentDirPath = /storage/emulated/0/Android/data/com.example.app_test_emasolar/files/Pictures/ticket
Uri contentUri = Uri.fromFile(dir);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);}
I also tried to add
android:requestLegacyExternalStorage="true"
to the manifest but nothing changed, I'm still stuck.
Any ideas?
Related
please help I could not add code, it is throwing error , I'm new.
ActivityResultLauncher<Intent> picActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// your operation....
Uri pic = data.getData();
profile.setImageURI(pic);
}
}
}
It depends on the device's android version. If it's 10 or lower than that, then you can simply save the file path in the Sharedpreference so you can access it later and load the image from there (You can do it in android 10 with requestLegacyExternalStorage of course or you can go with the 2nd option which I provided below).
But, if you're writing the code for android 11 or higher, then there are only three standard ways to do it.
1. Using SAF (Storage Access Framework) :-
You can get the storage access permission of that perticular folder everytime you pick an image from there. But this is not the best option when your app is doing it multiple times. (what if it's photo editor app or social media or something like that?!)
2. Manage all files permission :-
You can go with the All files access permission but it's also too much for the small task and you also have to give clarification to google play when your app has that permission. So it's also the very last option.
3. Accessing from internal app directories - THE BEST WAY! :-
You can go with this option with almost every app!
All you have to do is just take read storage permission, access the file using file descriptor, write it to the internal app directory (it can be either external files directory or cache directory), then you'll have a lifetime access of the image. You can save the path to Sharedpreference and access it anytime.
If you want to save edited image to the gallery then it will also be easy because you already have both read and write permission to that image saved in internal app directory.
That's it. I know the answer is lengthy but it's worth it. :)
This is a problem I found exclusively on Xiaomi Redmi Note 4 device (MIUI Global 8.1, Marshmallow)
So I'm making an app that generates a bitmap and saves it to FOOD folder. I have successfully generated the image and saved it to FOOD folder.
However, some of the images don't show on Gallery App. Specifically Xiaomi's Gallery App and Google's Photos. Note that some images DO show on Gallery App.
My question are:
How to show the images in Gallery after saving the image?
What exactly is causing this problem?
This really confuses me since it works on other devices with different OS.
This is what I have tried so far:
I've tried using the Intent.ACTION_MEDIA_SCANNER_SCAN_FILE and MediaScannerConnection.scanFile.
try {
String fileName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
MediaStore.Images.Media.insertImage(H5Environment.getContext().getContentResolver(), imagePath,
fileName, null);
} catch (Exception e) {
DanaLog.e(TAG, e);
}
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(new File(imagePath));
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
MediaScannerConnection.scanFile(this,
new String[]{imagePath},
new String[]{"image/png"},
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.d(TAG, "scan complete " + path);
}
});
I have also restarted the phone and it still doesn't show.
In Settings, I have enable Show hidden album but it doesn't show as well.
Current analysis:
I don't think there's a problem with the image itself because I can see it via File Manager.
The image path should also correct since some image is shown in the Gallery App.
Note:
I see a forum discussion on this and they suggest to 3rd party app such as Rescan SD Card!. But obviously, it was not the solution I was looking for.
try this for saveImage and notify Gallery
https://github.com/wuapnjie/StickerView/blob/master/sticker/src/main/java/com/xiaopo/flying/sticker/StickerUtils.java
I am trying to use the Microsoft word app available for android but cant seem to find the Intent options documented
I have a file and wish to open with Word:
private void editfile(final String file, final String field) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
Uri uri = Uri.parse(file);
intent.setDataAndType(uri,"appliction/*");
activity.startActivity(intent);
}
This launches Word, but doesn't open the selected file - in fact all the files in my storage are greyed out and not selectable (but can be selected if I run Word outside my app)
Is there an interface guide? anyone with any experience of using?
I'm attempting to send an image to Hangouts from within an app I'm building.
I'm working in Xamarin for VS 2015 to do this so the code below is c# but it's not much different from the equivalent Java code so I think it's easy to follow.
What I've done is set up a button on my app which has code setting up an Intent to share an image to Hangouts. I've set the image up already in the Downloads folder on the device and hardcoded the name into the code.
Intent hangoutsShareIntent = new Intent(Intent.ActionSend);
hangoutsShareIntent.SetType("image/jpeg");
hangoutsShareIntent.SetPackage("com.google.android.talk");
string downloadsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
string filePath = Path.Combine(downloadsPath, "shared.jpg");
hangoutsShareIntent.PutExtra(Intent.ExtraStream, filePath);
StartActivity(Intent.CreateChooser(hangoutsShareIntent, "Share with"));
When I run this, I get the option to select a chat in Hangouts that I want to send the content to. Upon selecting the chat, I get a blank message box and no image.
I've swapped the above code over to use text/plain and pass the filePath variable to the message. When I copy the file path into Chrome to check it, the image loads so I have to figure that the image is where I've said it is... right?
I get no errors (probably because the issue is in Hangouts rather than my app so I have nothing to debug there). Logcat shows nothing except an error I can't find much about on Google: ExternalAccountType﹕ Unsupported attribute readOnly
The only information I could find on that error implied some issue with permissions but I've made sure my app has runtime permissions checked for Read/Write using this code (which wraps the above):
if ((CheckSelfPermission(Permission.ReadExternalStorage) == (int)Permission.Granted) &&
(CheckSelfPermission(Permission.WriteExternalStorage) == (int)Permission.Granted))
NOTE: I'm running this on a HTC One M8 - no SD card but does have external storage on device. I've also added the above permissions to the manifest for earlier Android versions.
The documentation for this (here) isn't overly helpful either so any advice AT ALL here is welcome :)
Thanks!
If you use the file provider instead of sending just the URI on its own. This should get around the permission issues you are seeing.
There is a guide available here which might be useful.
Intent shareIntent = new Intent(Intent.ActionSend);
shareIntent.SetType("image/gif");
Java.IO.File file = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/myimage.gif");
Android.Net.Uri fileUri = Android.Support.V4.Content.FileProvider.GetUriForFile(this, "com.myfileprovider", file);
shareIntent.SetPackage("com.google.android.talk");
shareIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
shareIntent.PutExtra(Intent.ExtraStream, fileUri);
StartActivity(Intent.CreateChooser(shareIntent, "Share with"));
I am saving a bitmap . It saves to some root. But doesn't show in gallery. it show in gallery after restarting the device. I tried to send broadcast but it doesn't work for android 4.4.How can I do it to work for api 9-19
The built-in Gallery scans the phones memory when it thinks it's necessary. You can't force it to. Normaly restarting the gallery should help.
Edit:
I might have found an intent to refresh the gallery:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getApplicationContext().sendBroadcast(mediaScanIntent);
(f is the file wich has been added and should be scanned.) I haven't tried this yet, but it seems legit.