简体   繁体   English

Android Refresh图库强制关闭应用

[英]Android Refresh gallery force closes app

I have an issue refreshing the gallery on the android device. 我在刷新android设备上的图库时遇到问题。 Android device being used to debug with is a Galaxy Note 3 with NO sd card. 用于调试的Android设备是没有SD卡的Galaxy Note 3。

After deleting a file, a refresh has to be done on the entire gallery otherwise the image is still visible in the gallery of the phone even though it has been deleted (because there isn't a better way to do this for the deletion of a file to my knowledge) 删除文件后,必须在整个图库上进行刷新,否则即使删除了该图片,该图片在手机的图库中仍然可见(因为没有更好的方法可以删除手机据我所知)

But when I do the refresh the app force closes. 但是,当我进行刷新时,应用程序强制关闭。 I would guess that it has something to do with the phone not having a SD card to mount/unmount. 我想这可能与手机没有安装或卸载SD卡有关。

Is there a way to refresh the gallery besides this piece of code? 除了这段代码,还有其他方法可以刷新图库吗? Or do you know why the application would force close after the delete? 还是您知道为什么删除后应用程序会强制关闭?

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

I've also tried playing with that code as to change it to something like this, but still the error persists. 我也尝试过使用该代码将其更改为类似这样的内容,但是错误仍然存​​在。

     String selectedFile = getRealPathFromURI(selectedURI);
     me.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(selectedFile)));

    private String getRealPathFromURI(Uri contentURI) {
            Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
            if (cursor == null) { // Source is Dropbox or other similar local file path
                return contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            return cursor.getString(idx);
        }
    }

Also just for a last resort I tried this. 也是为了万不得已,我尝试了这个。 The app didn't crash, but didn't scan the file / or fix the problem either. 该应用程序没有崩溃,但是也没有扫描文件/也没有解决问题。

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

EDIT: I have added the permissions in the Manifest only for 编辑:我只在清单中添加了权限

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

Found how to fix this. 找到了解决方法。

You have to execute this on the main thread. 您必须在主线程上执行此操作。 There might be a way around this and if some one would add to this then it would be great. 可能有一种解决方法,如果有人对此加以补充,那就太好了。

So Fix : This has to be executed in your main activity for some reason. 因此修正:由于某种原因,必须在您的主要活动中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM