dodobird
dodobird

Reputation: 61

MediaScannerConnection scanFile scanner is crashing

When I run the following line of code:

MediaScannerConnection scanner = new MediaScannerConnection( this, null );
    scanner.connect();
    scanner.scanFile( szFinalFileName, null ); //<---crash here

I get a crash and this message in the console window:

09-26 14:47:44.074: ERROR/MediaScannerService(10288): Failed to delete file /data/data/com.android.providers.media/pause_scan

does anyone have any ideas why this isnt working and what i can do to fix it?

Thanks in advance :-)

Upvotes: 1

Views: 3546

Answers (2)

bable5
bable5

Reputation: 111

Another way to do it is to broadcast an intent and let the system handle it.

e.g.

public void scanFile(File downloadedFile){
    Uri contentUri = Uri.fromFile(downloadedFile);
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    mediaScanIntent.setData(contentUri);
    mContext.sendBroadcast(mediaScanIntent);
}

Upvotes: 9

dodobird
dodobird

Reputation: 61

I figured it out.

Here is the code:

final String szFile = szFinalFileName;

            m_pScanner = new MediaScannerConnection(this,
                    new MediaScannerConnectionClient() {
                        public void onMediaScannerConnected() {
                            m_pScanner.scanFile(szFile, null /*mimeType*/);
                        }

                        public void onScanCompleted(String path, Uri uri) {
                            if (path.equals(szFile)) {
                                MugMashView.this.runOnUiThread(new Runnable() {
                                    public void run() {
                                        Toast
                                            .makeText(getApplicationContext(),
                                                "Image now available in Home > Pictures",
                                                Toast.LENGTH_SHORT)
                                            .show();
                                    }
                                });
                                m_pScanner.disconnect();
                            }
                        }

                });
            m_pScanner.connect();

Upvotes: 5

Related Questions