How to show Images on Gallery App on Xiaomi MIUI 8.1? - java

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

Related

How to compress image before saving - for faster loading/display in RecyclerView?

So I am doing an app that lets you take images (inside CameraActivity, using CameraX) and saves them to local storage. The images are later displayed in a RecyclerView, getting them by their URI.
My issue is that as soon as I open the fragment that contains the recyclerview with the images it gets very very slow. The images are small and don't necessarily need to be in good quality, so I thought of maybe compressing them or just saving them in lower quality right when they are taken to make the loading process faster. Is there any way I can do that right here at this point in the code?
This is some relevant code from CameraActivity:
imgView_cameraTrigger.setOnClickListener(v -> {
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
File file = new File(getExternalFilesDir(Environment.DIRECTORY_DCIM), mDateFormat.format(new Date())+ ".jpg");
ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(file).build(); // saves image file
imageCapture.takePicture(outputFileOptions, executor, new ImageCapture.OnImageSavedCallback () {
#Override
public void onImageSaved(#NonNull ImageCapture.OutputFileResults outputFileResults) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
Toast.makeText(CameraActivity.this, "image saved successfully", Toast.LENGTH_SHORT).show();
// Toast.makeText(CameraActivity2.this, "PATH: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
Uri uri = Uri.fromFile(file);
ImageView preview = TrackFragment.getImageViewInTrackFragment();
preview.setImageURI(uri); // display taken image in imageview in track fragment
TrackFragment.setImgPreviewUri(uri); // pass uri to TrackFragment to use it there
CameraActivity.this.finish(); // exit camera activity
}
});
}
#Override
public void onError(#NonNull ImageCaptureException error) {
error.printStackTrace();
}
});
});
I thought of maybe compressing them or just saving them in lower quality right when they are taken to make the loading process faster
You probably can give resolution suggestions to CameraX to have it take a lower-resolution image, if that is what you mean. That would tie to code outside of your question.
However, a large part of your problem also lies here:
preview.setImageURI(uri); // display taken image in imageview in track fragment
The documentation for setImageURI() states:
This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup
In other words, using setImageURI() leads to poor performance and is rarely recommended.
Please use Glide, Picasso, or another image-loading library. Not only can they do the image loading on a background thread, but they can also automatically down-sample the image to fit your ImageView, improving loading speeds. They can also show a placeholder image while the image is being loaded, cache images in memory to reduce redundant image loads, etc.
FWIW, I demonstrate the use of Glide for image loading in this section of this free book, though changes in the US National Weather Service API may mean that the sample code itself does not work correctly now as it did when I published that final edition.

Getting geographical location of captured image - Android java

I'm new to android developement and I'm supposed to use Java as the programming language. I have an app where I'm supposed to be able to capture images and the geographical location of the captured images and display these details. I am displaying the image in an imageView. I have a text file where I'm storing image links as well as the captured images. So, I basically have image links and captured images that are stored in an arraylist then to a text file.
Please feel free to ask for anything that I may have missed out in the question.
I tried using EXIFInterface method I found on a Stack Overflow response, I tried using Location provider but to no avail. Maybe where I'm placing the code is incorrect, as I said, I'm new to this. I tried watching YT videos and did some research online and I'm more confused than ever at this point. Another approach I tried using was capturing the current location of the device to an invisible textView then calling it to where the image name is being stored but this did not work either.
The EXIF method I tried:
`
try {
ExifInterface exifInterface = new ExifInterface(direct); //Direct is the filepath
Log.d("Latitude", exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
Log.d("Longitude", exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
} catch (IOException e) {
e.printStackTrace();
}
`
Location Provider method
#SuppressLint("MissingPermission")
private void showLocation() {
locationProvider.getLastLocation().addOnSuccessListener(this,
new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
hiddenLoc.setText("Current location is: Lat:" + location.getLatitude()
+ "Lon: " + location.getLongitude());
}
}
});
}
`
EXIF location is an optional interface- most images won't have one. In fact many (most?) camera apps have stopped using it by default to protect user privacy. You can try it, but don't expect it to be there.
Your location code- lastLocation will return null unless location was already up and running (generally because another app was using it). You'd need to request location updates, rather than rely on lastLocation. Please note that this gets the location of the phone now not the location when a photo was taken. So this only works if you run it when you take the photo (the exif data gets the location where the photo was taken, if its there at all).

How to open specific tiktok video link in Android?

I basically want my button to open tiktok video link (link should be opened by tiktok app, not default browser or webview) on click. How to program that button in Android Studio?
maybe a late answer, but what you can do for not only TikTok, but any other app you want to open it from your Android application is to use the package name of it and open it using intent normally, example code to open Instagram for example.
url = "https://instagram.com/p/imagecode"
Uri uri = Uri.parse(url);
Intent likeIng = new Intent(Intent.ACTION_VIEW, URI);
likeIng.setPackage("com.instagram.android"); // -> here is the part
try {
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
//catch the Error if the app not installed.
}
you can get the package name from Google play URL itself it contains the package name, for TikTok, the package name is com.ss.android.ugc.trill so you can use it to open it using the above code, I did not test it with TikTok, but it's tested with Instagram, so I think the same logic applies.

Can't display pictures in the gallery (Android 10+)

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?

Android java. Saved image doesn't show in gallery immediately

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.

Categories