安卓相机:获取最近拍摄的照片缩略图做相册按钮图标

java 复制代码
String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media.DATE_TAKEN + " DESC");
if (cursor != null && cursor.moveToFirst()) {
    String path_camera = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
    while (true) {
        int index_id = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
        int index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        long id = cursor.getLong(index_id);
        String path = cursor.getString(index_data);
        if (path.startsWith(path_camera)) {
            Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null);
            imageButton_album.setImageBitmap(bitmap);
            break;
        } else
            cursor.moveToNext();
    }
    cursor.close();
}

拍完照要写入媒体数据库

复制代码
https://blog.51cto.com/vaero/779942
java 复制代码
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, filepath);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
MediaScannerConnection.scanFile(MainActivity.this, new String[] { filepath }, null, null);

但是,只能获取系统相机拍摄的照片,自己拍的照片取不到,数据库插入失败?

相关推荐
limuyang22 小时前
PDF Viewer KMP(基于 chrome 的 PDFium 内核)
android·kotlin
心平气和量大福大5 小时前
android-控件-搜索框SearchView
android·gitee
2601_962177135 小时前
【MySQL篇】聚合查询,联合查询
android·java·mysql
壮哥_icon6 小时前
【Android】批量 VectorDrawable 动态分别修改 fillColor 和 strokeColor 的踩坑与终极解决方案
android
齐天qaq7 小时前
Android 系统 APK 分区与权限
android
心平气和量大福大7 小时前
android-控件-多选框CheckBox
android·gitee
pengyu9 小时前
【Kotlin 协程修仙录 · 大乘境 · 初阶】 | 跳出三界:协程在 KMP 与后端开发中的跨平台之道
android·kotlin
XiaoLeisj9 小时前
Android Memory Profiler:堆内存指标、内存抖动与泄漏定位
android·性能优化·内存抖动·内存泄露·memory profiler
未来猫咪花9 小时前
Everything is ViewModel:让状态管理回到对象世界
android·flutter·ios