Android - Media Scanner not working on api 28-29
Asked Answered
P

4

8

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/*");
Pardew answered 16/8, 2019 at 7:6 Comment(0)
H
2

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>
Heterogeneous answered 19/2, 2020 at 11:49 Comment(0)
P
1

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

Planetstruck answered 19/2, 2020 at 8:1 Comment(0)
B
1
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();
            }
        }
Boudreaux answered 3/6, 2020 at 5:18 Comment(0)
E
0

Beginning with Android 9 (API level 28), The NETWORK_STATE_CHANGED_ACTION broadcast doesn't receive information about the user's location or personally identifiable data.

In addition, if your app is installed on a device running Android 9 or higher, system broadcasts from Wi-Fi don't contain SSIDs, BSSIDs, connection information, or scan results. To get this information, call getConnectionInfo() instead

for more info use this doc link : https://developer.android.com/guide/components/broadcasts

Expatriate answered 19/2, 2020 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.