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

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);

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

相关推荐
simplepeng18 分钟前
我们都知道但总是忽略的5个Jetpack Compose细节
android·android jetpack
刮风那天1 小时前
Android 如何提高进程优先级避免被查杀?
android
修行者对6662 小时前
安卓阿里云镜像
android
刮风那天3 小时前
Android AMS创建进程不用Binder而用Socket?
android·java·binder
知行合一。。。5 小时前
Python--05--面向对象(继承,多态)
android·开发语言·python
张小潇6 小时前
AOSP15 WMS/AMS系统开发 -窗口动画源码分析
android
程序员陆业聪7 小时前
Shadow核心原理:壳子Activity与代理机制的精妙设计
android
plainGeekDev8 小时前
Android 开发者再不转Kotlin,真的来不及了
android·kotlin
赏金术士8 小时前
第五章:数据层—网络请求与Repository
android·kotlin·compose
初雪云8 小时前
让安卓发版再简单一点,体验一键自动化发布
android·运维·自动化