media数据库操作,可以进行增删改查,实现回收站,隐私照片功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38998213/article/details/82149682
这里面有假删除也就是放入回收站;获取假删除文件即回收站文件;真正删除文件;恢复回收站文件;
回收站与隐私空间类似,原理一样,只不过叫法不同。


其中假删除的操作原理是:首先获取文件路径,对文件命名前加“.”,进行文件重命名,将文件路径进行重命名标记,保存到sp中。使用Contentresolver对数据库进行删除操作,这样我们使用cursor对media数据库查询就不会显示已经删除的数据。

获取回收站或者隐私空间的文件是通过sp文件中存的数据进行获取的。

/*删数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } 

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;

import com.blankj.utilcode.util.SPUtils;
import com.example.john.greendaodemo.DBManagerClassifiy;
import com.nick.albummanagement.database.bean.AlbumBean;
import com.nick.albummanagement.database.bean.PhotoUpImageItem;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by
 * Stone on 2018/8/10.
 */

public class HiddenHelper {

    public static final String PHOTO_DUMP_ALL = "PHOTO_DUMP_ALL_HIDDEN";
    public static final String VIDEO_DUMP_ALL = "VIDEO_DUMP_ALL_HIDDEN";
    public static final String PHOTO_SP_NAME = "PHOTO_SP_NAME_HIDDEN";
    public static final String VIDEO_SP_NAME = "VIDEO_SP_NAME_HIDDEN";
    private static HiddenHelper instance ;
    private HiddenHelper() {
    }
    public static HiddenHelper getInsatnce(){
        if (instance == null){
            synchronized (HiddenHelper.class){
                if (instance == null){
                    instance = new HiddenHelper();
                }
            }
        }
        return instance;
    }
    @NonNull
    public List<AlbumBean> getDeletePhoto(){
        List<AlbumBean> result = new ArrayList<>();
        String dumpPhoto = getDumpPhoto();
        String[] files = dumpPhoto.split("\\|");
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isEmpty()) {
                String[] timeAndPath = files[i].split("\\*");
                long time;
                try {
                    time = Long.parseLong(timeAndPath[0]);
                } catch (NumberFormatException e) {
                    time = 0;
                } catch (Exception e) {
                    time = 0;
                }
                String path = timeAndPath[1];
                //String strTime = timeAndPath[0];
                //photoFiles.add(new MyFile(path, false, 1, time, strTime));
                File file = new File(path);
                if (file!= null && file.exists()){
                    AlbumBean bean = new AlbumBean();
                    bean.setPath(path);
                    bean.setAddDate(""+time);
                    bean.setFlag(0);
                    result.add(bean);
                }else{
                    removeDumpPhoto(files[i]);
                }
            }else{

            }
        }
        return result;
    }
    @NonNull
    public List<AlbumBean> getDeleteVideo(){
        List<AlbumBean> result = new ArrayList<>();
        String dumpPhoto = getDumpVideo();
        String[] files = dumpPhoto.split("\\|");
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isEmpty()) {
                String[] timeAndPath = files[i].split("\\*");
                long time;
                try {
                    time = Long.parseLong(timeAndPath[0]);
                } catch (NumberFormatException e) {
                    time = 0;
                } catch (Exception e) {
                    time = 0;
                }
                String path = timeAndPath[1];
                //String strTime = timeAndPath[0];
                //photoFiles.add(new MyFile(path, false, 1, time, strTime));
                File file = new File(path);
                if (file!= null && file.exists()){
                    AlbumBean bean = new AlbumBean();
                    bean.setPath(path);
                    bean.setAddDate(""+time);
                    bean.setFlag(1);
                    result.add(bean);
                }else{
                    removeDumpPhoto(files[i]);
                }
            }else{

            }
        }
        return result;
    }

    //deletePhotoToBinByTag
    public boolean deletePhotoToBin(@NonNull Context context, @NonNull AlbumBean albumBean,long time){
        String imgPath = albumBean.getPath();
        //String name = albumBean.getDisplayName();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
        File file = new File(imgPath);
        String name = file.getName();
        String newPath = file.getAbsolutePath().replace(name, "." + name);
        file.renameTo(new File(newPath));
        saveDumpPhoto( time + "*" + newPath);
        DBManagerClassifiy.getInstance(context).deleteTagPhotoByPath(albumBean.getPath());
        System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }
    public boolean deleteToBinByType(@NonNull Context context, @NonNull AlbumBean albumBean,long time){
        Log.e("AAA", "deleteToBinByType: ----->"+albumBean.getPath() );
        if (albumBean.getType() != null  ){

            if ( albumBean.getType().contains("image")){
                String imgPath = albumBean.getPath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
                File file = new File(imgPath);
                String name = file.getName();
                String newPath = file.getAbsolutePath().replace(name, "." + name);
                file.renameTo(new File(newPath));
                saveDumpPhoto( time + "*" + newPath);
                DBManagerClassifiy.getInstance(context).deleteTagPhotoByPath(albumBean.getPath());
        /*删除数据库记录*/
                ContentResolver resolver = context.getContentResolver();
                Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                        new String[]{imgPath}, null);
                boolean result = false;
                try {
                    if (cursor.moveToFirst()) {
                        long id = cursor.getLong(0);
                        Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                        Uri uri = ContentUris.withAppendedId(contentUri, id);
                        int count = context.getContentResolver().delete(uri, null, null);
                        result = count == 1;
                    } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                } catch (NullPointerException e) {
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                }
                //video
            }else if (albumBean.getType().contains("video")){
                String imgPath = albumBean.getPath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
                File file = new File(imgPath);
                String name = file.getName();
                String newPath = file.getAbsolutePath().replace(name, "." + name);
                file.renameTo(new File(newPath));
                saveDumpVideo( time + "*" + newPath);
                System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
                ContentResolver resolver = context.getContentResolver();
                Cursor cursor = MediaStore.Images.Media.query(resolver,  MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media._ID}, MediaStore.Video.Media.DATA + "=?",
                        new String[]{imgPath}, null);
                boolean result = false;
                try {
                    if (cursor.moveToFirst()) {
                        long id = cursor.getLong(0);
                        Uri contentUri =MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                        Uri uri = ContentUris.withAppendedId(contentUri, id);
                        int count = context.getContentResolver().delete(uri, null, null);
                        result = count == 1;
                    } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                } catch (NullPointerException e) {
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                }
            }
        }
        return false;
    }

    public boolean deletePhotoToBinByType(@NonNull Context context, @NonNull AlbumBean albumBean,long time){
        if (albumBean.getType() != null  ){
            //  pic
            if ( albumBean.getType().equals("0")){
                String imgPath = albumBean.getPath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
                File file = new File(imgPath);
                String name = file.getName();
                String newPath = file.getAbsolutePath().replace(name, "." + name);
                file.renameTo(new File(newPath));
                saveDumpPhoto( time + "*" + newPath);
                DBManagerClassifiy.getInstance(context).deleteTagPhotoByPath(albumBean.getPath());
                System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
                ContentResolver resolver = context.getContentResolver();
                Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                        new String[]{imgPath}, null);
                boolean result = false;
                try {
                    if (cursor.moveToFirst()) {
                        long id = cursor.getLong(0);
                        Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                        Uri uri = ContentUris.withAppendedId(contentUri, id);
                        int count = context.getContentResolver().delete(uri, null, null);
                        result = count == 1;
                    } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                } catch (NullPointerException e) {
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                }
                //video
            }else if (albumBean.getType().equals("0")){
                String imgPath = albumBean.getPath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
                File file = new File(imgPath);
                String name = file.getName();
                String newPath = file.getAbsolutePath().replace(name, "." + name);
                file.renameTo(new File(newPath));
                saveDumpVideo( time + "*" + newPath);
                System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
                ContentResolver resolver = context.getContentResolver();
                Cursor cursor = MediaStore.Images.Media.query(resolver,  MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media._ID}, MediaStore.Video.Media.DATA + "=?",
                        new String[]{imgPath}, null);
                boolean result = false;
                try {
                    if (cursor.moveToFirst()) {
                        long id = cursor.getLong(0);
                        Uri contentUri =MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                        Uri uri = ContentUris.withAppendedId(contentUri, id);
                        int count = context.getContentResolver().delete(uri, null, null);
                        result = count == 1;
                    } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                } catch (NullPointerException e) {
                    if (cursor != null) {
                        if (!cursor.isClosed()) {
                            cursor.close();
                        }
                    }
                    return result;
                }
            }
        }
        return false;
    }

    public boolean deletePhotoToBinByTag(@NonNull Context context, @NonNull AlbumBean albumBean,long time){
        Log.e("AAA", "deletePhotoToBinByTag: "+albumBean.getPath() );
        //  pic
        if (albumBean.getFlag() ==0){
            String imgPath = albumBean.getPath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
            File file = new File(imgPath);
            String name = file.getName();
            String newPath = file.getAbsolutePath().replace(name, "." + name);
            file.renameTo(new File(newPath));
            saveDumpPhoto( time + "*" + newPath);
            DBManagerClassifiy.getInstance(context).deleteTagPhotoByPath(albumBean.getPath());
            System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
            ContentResolver resolver = context.getContentResolver();
            Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                    new String[]{imgPath}, null);
            boolean result = false;
            try {
                if (cursor.moveToFirst()) {
                    long id = cursor.getLong(0);
                    Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    Uri uri = ContentUris.withAppendedId(contentUri, id);
                    int count = context.getContentResolver().delete(uri, null, null);
                    result = count == 1;
                } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
                if (cursor != null) {
                    if (!cursor.isClosed()) {
                        cursor.close();
                    }
                }
                return result;
            } catch (NullPointerException e) {
                if (cursor != null) {
                    if (!cursor.isClosed()) {
                        cursor.close();
                    }
                }
                return result;
            }
            //video
        }else if (albumBean.getFlag() ==1){
            String imgPath = albumBean.getPath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
            File file = new File(imgPath);
            String name = file.getName();
            String newPath = file.getAbsolutePath().replace(name, "." + name);
            file.renameTo(new File(newPath));
            saveDumpVideo( time + "*" + newPath);
            System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
            ContentResolver resolver = context.getContentResolver();
            Cursor cursor = MediaStore.Images.Media.query(resolver,  MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media._ID}, MediaStore.Video.Media.DATA + "=?",
                    new String[]{imgPath}, null);
            boolean result = false;
            try {
                if (cursor.moveToFirst()) {
                    long id = cursor.getLong(0);
                    Uri contentUri =MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    Uri uri = ContentUris.withAppendedId(contentUri, id);
                    int count = context.getContentResolver().delete(uri, null, null);
                    result = count == 1;
                } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
                if (cursor != null) {
                    if (!cursor.isClosed()) {
                        cursor.close();
                    }
                }
                return result;
            } catch (NullPointerException e) {
                if (cursor != null) {
                    if (!cursor.isClosed()) {
                        cursor.close();
                    }
                }
                return result;
            }
        }

        return false;
    }

    public boolean deleteSimilarPhotoToBin(@NonNull Context context, @NonNull PhotoUpImageItem albumBean, long time){
        String imgPath = albumBean.getImagePath();
        String name = albumBean.getDisPlayName();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
        File file = new File(imgPath);
        String newPath = file.getAbsolutePath().replace(name, "." + name);
        file.renameTo(new File(newPath));
        saveDumpPhoto( time + "*" + newPath);
        System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }
    public boolean deleteSimilarPhotoReal(@NonNull Context context, @NonNull PhotoUpImageItem albumBean){
        String imgPath = albumBean.getImagePath();

        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
        File file = new File(imgPath);
        file.delete();
        /*删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }

    public boolean deleteTagPhotoToBin(@NonNull Context context, @NonNull PhotoUpImageItem albumBean, long time){
        String imgPath = albumBean.getImagePath();
        // String name = albumBean.getDisPlayName();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
        File file = new File(imgPath);
        String name = file.getName();
        String newPath = file.getAbsolutePath().replace(name, "." + name);
        file.renameTo(new File(newPath));
        saveDumpPhoto( time + "*" + newPath);
        System.out.println("PhotoCleanActivity.HideFile" + newPath + "sdfasdf" + file.getAbsolutePath());
        /*删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            DBManagerClassifiy.getInstance(context).deleteTagPhotoByPath(albumBean.getImagePath());
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }

    public boolean deleteTagPhotoReal(@NonNull Context context, @NonNull PhotoUpImageItem albumBean){
        String imgPath = albumBean.getImagePath();
        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
        File file = new File(imgPath);
        file.delete();
        /*删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            DBManagerClassifiy.getInstance(context).deleteTagPhotoByPath(albumBean.getImagePath());
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }

    public boolean deletePhotoReal(@NonNull Context context, @NonNull AlbumBean albumBean){
        String imgPath = albumBean.getPath();

        /*文件名前面加. 隐藏 若需要显示就去掉.同时保存到sharedprefence中*/
        File file = new File(imgPath);
        file.delete();
        /*删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } /*else {
                File file = new File(imgPath);
                result = file.delete();
            }*/
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }
    /*保存回收站图片的path*/
    private   void saveDumpPhoto(@NonNull String path) {
        SPUtils.getInstance(PHOTO_SP_NAME).put(
                PHOTO_DUMP_ALL, getDumpPhoto() + "|" + path);
    }

    /*获取回收站图片的path*/
    private   String getDumpPhoto() {
        return SPUtils.getInstance(PHOTO_SP_NAME).getString( PHOTO_DUMP_ALL, "");
    }

    /*删除回收站中某一个图片的path*/
    private   void removeDumpPhoto(@NonNull String path) {
        SPUtils.getInstance(PHOTO_SP_NAME).put( PHOTO_DUMP_ALL,
                getDumpPhoto().replace(path, ""));
    }

    /*保存回收站video的path*/
    private   void saveDumpVideo(@NonNull String path) {
        SPUtils.getInstance(VIDEO_SP_NAME).put(
                VIDEO_DUMP_ALL, getDumpVideo() + "|" + path);
    }

    /*获取回收站video的path*/
    private   String getDumpVideo() {
        return SPUtils.getInstance(VIDEO_SP_NAME).getString( VIDEO_DUMP_ALL, "");
    }

    /*删除回收站中某一个video的path*/
    private   void removeDumpVideo(@NonNull String path) {
        SPUtils.getInstance(VIDEO_SP_NAME).put( VIDEO_DUMP_ALL,
                getDumpVideo().replace(path, ""));
    }

    public boolean recoverPhotoFile(@NonNull Context context, @NonNull String imgPath, @NonNull String date) throws Exception{
         /*文件名前面加. 隐藏 若需要显示就去掉.*/
        removeDumpPhoto(date+"*"+imgPath);
        File file = new File(imgPath);
        String name = file.getName();
        String newName = file.getName().substring(1, file.getName().length());
        String newPath = file.getAbsolutePath().replace(name, newName);
        boolean result = false;
        result = file.renameTo(new File(newPath));
        // 创建ContentValues对象,准备插入数据
        int lastIndex = newName.lastIndexOf(".");
        String ext = newPath.substring(lastIndex + 1).toLowerCase();
        if (ext.equals("jpg")) {
            ext = "jpeg";
        }
        String simple = newName.substring(0, lastIndex);
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, simple);
        values.put(MediaStore.Images.Media.DISPLAY_NAME, simple);
        values.put(MediaStore.Images.Media.DESCRIPTION, simple);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/" + ext);
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.DATA, newPath);
        try {
            context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } catch (Exception e) {
            e.printStackTrace();
            //Crashlytics.logException(e);
        }
        return result;
    }

    public boolean recoverVideoFile(@NonNull Context context, @NonNull String imgPath, @NonNull String date) throws Exception{
         /*文件名前面加. 隐藏 若需要显示就去掉.*/
        removeDumpPhoto(date+"*"+imgPath);
        File file = new File(imgPath);
        String name = file.getName();
        String newName = file.getName().substring(1, file.getName().length());
        String newPath = file.getAbsolutePath().replace(name, newName);
        boolean result = false;
        result = file.renameTo(new File(newPath));
        // 创建ContentValues对象,准备插入数据
        int lastIndex = newName.lastIndexOf(".");
        String ext = newPath.substring(lastIndex + 1).toLowerCase();
        String simple = newName.substring(0, lastIndex);
        ContentValues values = new ContentValues();
        values.put(MediaStore.Video.Media.TITLE, simple);
        values.put(MediaStore.Video.Media.DISPLAY_NAME, simple);
        values.put(MediaStore.Video.Media.DESCRIPTION, simple);
        values.put(MediaStore.Video.Media.MIME_TYPE, "video/" + ext);
        values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(MediaStore.Video.Media.DATA, newPath);
        try {
            context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
        } catch (Exception e) {
            e.printStackTrace();
            //Crashlytics.logException(e);
        }
        return result;
    }

    private boolean deleteImageReally(@NonNull Context context,@NonNull String path,@NonNull String date) throws Exception{
        /*去除Sharedpreference中的记录*/
        // RecycleBinUtil.removeDumpPhoto(mView.getActivityContext(),);
        removeDumpPhoto(date+"*"+path);
        /*删除文件并删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{path}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } else {
                File file = new File(path);
                result = file.delete();
            }
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }

    private boolean deleteVideoReally(@NonNull Context context,@NonNull String path,@NonNull String date) throws Exception{
        /*去除Sharedpreference中的记录*/
        // RecycleBinUtil.removeDumpPhoto(mView.getActivityContext(),);
        removeDumpVideo(date+"*"+path);
        /*删除文件并删除数据库记录*/
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media._ID}, MediaStore.Video.Media.DATA + "=?",
                new String[]{path}, null);
        boolean result = false;
        try {
            if (cursor.moveToFirst()) {
                long id = cursor.getLong(0);
                Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                Uri uri = ContentUris.withAppendedId(contentUri, id);
                int count = context.getContentResolver().delete(uri, null, null);
                result = count == 1;
            } else {
                File file = new File(path);
                result = file.delete();
            }
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        } catch (NullPointerException e) {
            if (cursor != null) {
                if (!cursor.isClosed()) {
                    cursor.close();
                }
            }
            return result;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38998213/article/details/82149682