jeudi 25 août 2016

reflect call MediaScanner.scanDirectories() throw InvocationTargetException

all my code is like below:

public void mediaScan(View source) {
    String[] path = new String[]{"/storage/F0E9-334E/Wildlife.wmv"};
    try {
        Button button = (Button) findViewById(R.id.my_btn);
        Class<?> mediaScannerClass = Class.forName("android.media.MediaScanner");
        Object mediaScanner = mediaScannerClass.getConstructor(Context.class).newInstance(this);
        Method scanDirectoryMethod = mediaScannerClass.getMethod("scanDirectories",
            path.getClass(), String.class);
        scanDirectoryMethod.invoke(mediaScanner, path, "external");//this line throw a InvocationTargetException everytime***
    } catch (Exception e) {
        e.printStackTrace();
        if (e instanceof InvocationTargetException) {
            Throwable targetEx = ((InvocationTargetException) e).getTargetException();
            if (targetEx != null) {}
        }else{}
    }

Every time call scanDirectoryMethod.invoke(mediaScanner, path, "external") will catch a InvocationTargetException like

java.lang.SecurityException: Permission Denial: 
reading com.android.providers.media.MediaProvider uri content://media/external/file?limit=1000 from pid=6587, uid=10105 
requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

I've add the permission in AndroidManifest.xml like below,but it help nothing.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

Anyone could help me out ? Thanks





Aucun commentaire:

Enregistrer un commentaire