android 将图片始终保持在相册中的最顶部

先 根据文件绝对路径获取URI

/***
     * 根据文件绝对路径获取URI
     */
    public static Uri findImageUri(String picPath, Activity act) {
        Uri mUri = Uri.parse("content://media/external/images/media");
        Uri mImageUri = null;
        Cursor cursor = act.managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Images.Media.DEFAULT_SORT_ORDER);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            String data = cursor.getString(cursor
                    .getColumnIndex(MediaStore.MediaColumns.DATA));
            if (picPath.equals(data)) {
                int ringtoneID = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
                String dateAdd = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATE_ADDED));
                String dateModify = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED));
                Logger.e( "    " + dateAdd + "    " + dateModify);
                mImageUri = Uri.withAppendedPath(mUri, "" + ringtoneID);
                break;
            }
            cursor.moveToNext();
        }
        return mImageUri;
    }

再根据URI 修改数据库

 /***将图片保存在相册中的最顶部**/
    public static void updateQrcodeToHead(Context mContext, String imgPath, String fileName, Uri imageUri) {
        // 系统时间
        long mImageTime = System.currentTimeMillis();
        long dateSeconds = mImageTime / 1000;
        // 保存截屏到系统MediaStore
        ContentValues values = new ContentValues();
        ContentResolver resolver = mContext.getContentResolver();

        values.put(MediaStore.Images.ImageColumns.DATA, imgPath);
        values.put(MediaStore.Images.ImageColumns.TITLE, "企业二维码");
        values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, fileName);
        values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
        values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds);
        values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds);
        values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
        values.put(MediaStore.Images.ImageColumns.WIDTH, 396);
        values.put(MediaStore.Images.ImageColumns.HEIGHT, 396);
        resolver.update(imageUri, values, null, null);
    }

你可能感兴趣的:(android 将图片始终保持在相册中的最顶部)