I'm scanning files using MediaScanner
. But API 28 and 29 don't work.
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, file));
I used Broadcast
, but it didn't work the same way. Because it is "deprecated".
java public static class FilesScanner extends ContextWrapper implements MediaScannerConnection.MediaScannerConnectionClient{
private MediaScannerConnection connection = null;
private String SCAN_FILE_PATH = "";
private String SCAN_FILE_MIME_TYPE = "*/*";
public static FilesScanner with(Context context, String SCAN_FILE_PATH){ return new FilesScanner(context, SCAN_FILE_PATH, "*/*"); }
public static FilesScanner with(Context context, String SCAN_FILE_PATH, String SCAN_FILE_MIME_TYPE){ return new FilesScanner(context, SCAN_FILE_PATH, SCAN_FILE_MIME_TYPE); }
private FilesScanner(Context context, String SCAN_FILE_PATH, String SCAN_FILE_MIME_TYPE){
super(context);
this.SCAN_FILE_PATH = SCAN_FILE_PATH;
if (SCAN_FILE_PATH == null){
this.SCAN_FILE_PATH = FilePaths.ROOT_DIR;
}
this.SCAN_FILE_MIME_TYPE = SCAN_FILE_MIME_TYPE;
if (SCAN_FILE_MIME_TYPE == null){
this.SCAN_FILE_MIME_TYPE = "*/*";
}
connection = new MediaScannerConnection(this, this);
connection.connect();
}
@Override
public void onMediaScannerConnected() {
MyLog.d("onMediaScannerConnected");
connection.scanFile(SCAN_FILE_PATH, SCAN_FILE_MIME_TYPE);
}
@Override
public void onScanCompleted(String path, Uri uri) {
MyLog.d("onScanCompleted" + " URI : " + uri.getPath());
connection.disconnect();
}
}
FilesScanner.with(this, path, "image/*");
Did you add this to the manifest file?
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${app_package_name}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Have you defined FileProvider
entry in your AndroidManifest.xml
?
You need to specify <files-path>
in that you need to make available to other apps.
Refer this file sharing training and FileProvider reference
i used this code to crop image but its not working in api level 28 and 29 can anyone suggest where getting wrong.
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
tempDir.mkdir();
File tempFile = null;
try {
tempFile = File.createTempFile("Trendii", ".png", tempDir);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte[] bitmapData = bytes.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bitmapData);
fos.flush();
fos.close();
float height = 0, width = 0;
if (bitmap != null) {
height = bitmap.getHeight();
width = bitmap.getWidth();
}
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(Uri.fromFile(tempFile), destination, inspirationId, height, width).asSquare().start(InspirationDetailsActivity.this);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
showSettingsDialog();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
}).check();
} else {
File tempDir = Environment.getExternalStorageDirectory();
tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
tempDir.mkdir();
File tempFile = null;
try {
tempFile = File.createTempFile("Trendii", ".png", tempDir);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
byte[] bitmapData = bytes.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(bitmapData);
fos.flush();
fos.close();
float height = 0, width = 0;
if (bitmap != null) {
height = bitmap.getHeight();
width = bitmap.getWidth();
}
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(Uri.fromFile(tempFile), destination, inspirationId, height, width).asSquare().start(this);
} catch (IOException e) {
e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With