111.Android简单的监听媒体库,查询媒体库图片、音频、文档文件

111.Android简单的监听媒体库,查询媒体库图片、音频、文档文件,第1张

111.Android简单的监听媒体库查询媒体库图片、音频、文档文件 1.媒体库监听:

public class MediaStoreChangeObserver extends ContentObserver {
    private static final String TAG = "MediaStoreChangeObserver";
    private volatile static MediaStoreChangeObserver mediaStoreChangeObserver = null;

    public static MediaStoreChangeObserver getInstance() {
        if (null == mediaStoreChangeObserver) {
            synchronized (MediaStoreChangeObserver.class) {
                if (null == mediaStoreChangeObserver) {
                    mediaStoreChangeObserver = new MediaStoreChangeObserver();
                }
            }
        }
        return mediaStoreChangeObserver;
    }

    private MediaStoreChangeObserver() {
        super(new Handler());
        RecordApp.context.getContentResolver().registerContentObserver(MediaStore.Files.getContentUri("external"), true, this);
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);
        XLog.d(TAG, "selfChange:" + selfChange + " Uri:" + uri);
       
    }

}
2.查询图片文件、查询音频文件、查询文档文件:
 
    private List getAllPhoto(Context context) {
        String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DISPLAY_NAME, MediaStore.Images.ImageColumns.MIME_TYPE};
//        //相当于我们常用sql where 后面的写法
//        String selection = MediaStore.Images.ImageColumns.MIME_TYPE + "= ? "
//                + " or " + MediaStore.Images.ImageColumns.MIME_TYPE + " = ? "
//                + " or " + MediaStore.Images.ImageColumns.MIME_TYPE + " = ? "
//                + " or " + MediaStore.Images.ImageColumns.MIME_TYPE + " = ? "
//                + " or " + MediaStore.Images.ImageColumns.MIME_TYPE + " = ? "
//                + " or " + MediaStore.Images.ImageColumns.MIME_TYPE + " = ? "
//                + " or " + MediaStore.Images.ImageColumns.MIME_TYPE + " = ? ";
//
//        String[] strings = {"jpg", "svg", "png", "webp", "jpeg", "gif", "bmp"};

        //asc 按升序排列
        //    desc 按降序排列
        //projection 是定义返回的数据,selection 通常的sql 语句,例如  selection=MediaStore.Images.ImageColumns.MIME_TYPE+"=? " 那么 selectionArgs=new String[]{"jpg"};
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns.DATE_MODIFIED + "  desc");
        List photos = new ArrayList<>();
        if (null == cursor) {
            return photos;
        }
        while (cursor.moveTonext()) {
            String fileId = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID));
            String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));
            String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
            String fileType = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.MIME_TYPE));
            Log.e(TAG, "(" + fileId + ")(" + fileName + ")(" + filePath + ")" + "(" + fileType + ")");
            FileBean fileBean = new FileBean();
            fileBean.setFilePath(filePath);
            fileBean.setFileType(1);
            photos.add(fileBean);
        }
        cursor.close();
        return photos;
    }

    
    private List getAudioFiles(Context context) {
        String[] projection = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE, MediaStore.Files.FileColumns.MIME_TYPE};
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Audio.AudioColumns.DATE_MODIFIED + "  desc");
        if (null == cursor) {
            return new ArrayList<>();
        }
        List audioList = new ArrayList<>();
        while (cursor.moveTonext()) {
            String fileId = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID));
            String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE));
            String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
            String fileType = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE));
            Log.e(TAG, "(" + fileId + ")(" + fileName + ")(" + filePath + ")" + "(" + fileType + ")");
            FileBean fileBean = new FileBean();
            fileBean.setFilePath(filePath);
            fileBean.setFileType(2);
            audioList.add(fileBean);
        }
        cursor.close();
        return audioList;
    }

    
    private List getAllText(Context context) {
        String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE};
//        //相当于我们常用sql where 后面的写法
        String selection = MediaStore.Files.FileColumns.MIME_TYPE + "= ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "
                + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? ";


        //(xls)  application/vnd.ms-excel
        //(csv) text/comma-separated-values
        //(doc) application/msword
        //(ppt) application/vnd.ms-powerpoint
        //(pdf) application/pdf
        //(txt) text/plain
        //(zip) application/zip
        //(rar) application/rar
        //(xlsx) application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
        //(docx) application/vnd.openxmlformats-officedocument.wordprocessingml.document


        String[] selectionArgs = new String[]{"application/vnd.ms-excel",
                "text/comma-separated-values",
                "application/msword",
                "application/vnd.ms-powerpoint",
                "application/pdf",
                "text/plain",
                "application/zip",
                "application/rar",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
        Cursor cursor = context.getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, selection, selectionArgs, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        if (null == cursor) {
            return new ArrayList<>();
        }
        List texts = new ArrayList<>();
        while (cursor.moveTonext()) {
            String fileId = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID));
            String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE));
            String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
            String fileType = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE));
            Log.e(TAG, "(" + fileId + ")(" + fileName + ")(" + filePath + ")" + "(" + fileType + ")");
            FileBean fileBean = new FileBean();
            fileBean.setFilePath(filePath);
            fileBean.setFileType(3);
            texts.add(fileBean);
        }
        cursor.close();
        return texts;
    }

3.更新媒体库文件,往媒体库里插入文件,例如:

private void updateMedia(String path, Context context) {
    ContentValues values = new ContentValues();
    //媒体文件的标题
    values.put(MediaStore.Audio.AudioColumns.TITLE, "录音媒体文件");
    //时间戳
    values.put(MediaStore.Audio.AudioColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
    //文件类型 统一通配符
    values.put(MediaStore.Audio.AudioColumns.MIME_TYPE, "audio/*");
    //指定文件路径。必须是绝对路径
    values.put(MediaStore.Audio.Media.DATA, path);
    //把文件插入到媒体库ContentProvider中
    ContentResolver resolver = context.getContentResolver();
    Uri uri = resolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);
    //发送广播。通知此媒体文件已经可以用啦
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
}

4.FileBean:
public class FileBean {




    private String filePath;//文件路径


    private int fileType;//文件类型 1是图片 2是录音 3是文件


    private boolean isUpload;//是否已经上传

    @Generated(hash = 1718135030)
    public FileBean( String filePath, int fileType
                  ) {

        this.filePath = filePath;
        this.fileType = fileType;

    }

    @Generated(hash = 1910776192)
    public FileBean() {
    }


    public String getFilePath() {
        return this.filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public int getFileType() {
        return this.fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }

    public boolean getIsUpload() {
        return this.isUpload;
    }

    public void setIsUpload(boolean isUpload) {
        this.isUpload = isUpload;
    }

    @Override
    public String toString() {
        return "{" +
                "filePath='" + filePath + ''' +
                ", fileType=" + fileType +
                ", isUpload=" + isUpload +
                '}';
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5696872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存