如何从光标中仅获取jpg或png图像

时间:2017-11-06 13:09:17

标签: android image cursor

我只想要带有光标特定扩展名的图片。 如果我写这样的游标查询,那么它会给我最后添加的图像。 例如,如果我使用jpg图像,那么它将提供.jpg图像路径。 如果我添加.png扩展名,那么它将提供.png图像路径

Cursor cur = getContentResolver().query(Images, PROJECTION_BUCKET, "1) GROUP BY 1,(2", null,
                "MAX(datetaken) DESC");

但是如何从光标获取特定于图像文件的扩展名。 我试过这段代码但是给了我一个错误:

String[] PROJECTION_BUCKET = new String[] { "bucket_id", "bucket_display_name", "datetaken", "_data" };
Uri Images = Media.EXTERNAL_CONTENT_URI;


String where = MediaStore.Images.Media.MIME_TYPE + "='image/jpeg'" +
                " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/png’"+
                        " OR " + MediaStore.Images.Media.MIME_TYPE + "='image/jpg’";


Cursor cur = getContentResolver().query(Images, PROJECTION_BUCKET where,null,MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");

错误:

11-06 18:15:10.249: E/SQLiteLog(3167): (1) near "image": syntax error
11-06 18:15:10.249: E/DatabaseUtils(3167): Writing exception to parcel
11-06 18:15:10.249: E/DatabaseUtils(3167): android.database.sqlite.SQLiteException: near "image": syntax error (code 1): , while compiling: SELECT bucket_id, bucket_display_name, datetaken, _data FROM images WHERE (mime_type='image/jpeg' OR mime_type='image/png’ OR mime_type='image/jpg’) ORDER BY datetaken DESC
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:333)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at com.android.providers.media.MediaProvider.query(MediaProvider.java:2700)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProvider.query(ContentProvider.java:1010)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProvider$Transport.query(ContentProvider.java:214)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
11-06 18:15:10.249: E/DatabaseUtils(3167):  at android.os.Binder.execTransact(Binder.java:446)

有人知道怎么写这个查询吗?

3 个答案:

答案 0 :(得分:0)

正确的查询在我下面找到了我自己的问题的解决方案

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    // Do the file write
} else {
    // Request permission from the user
    ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    ...
}

我的问题代码中只有一个错误,错误是我在“png”和“jpg”结束时写了这个'我只是将'this'替换为'它是

答案 1 :(得分:0)

对于仍然在研究这个问题的人来说,这是解决方案:

private String[] thumbColumns = {MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID};
private String[] columns = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN};
private String[] whereArgs = {"image/jpeg", "image/png", "image/jpg"};

String orderBy = MediaStore.Images.Media.DATE_TAKEN+ " DESC";
String  where = MediaStore.Images.Media.MIME_TYPE + "=? or "
                + MediaStore.Images.Media.MIME_TYPE + "=? or "
                + MediaStore.Images.Media.MIME_TYPE + "=?";

Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, where, whereArgs, orderBy);

答案 2 :(得分:0)

 private fun getLocalPhotos(contentResolver: ContentResolver): ArrayList<Uri> {
    val projection = arrayOf(MediaStore.Images.Media._ID)
    val selection = (MediaStore.Images.Media.MIME_TYPE + "=? or "
            + MediaStore.Images.Media.MIME_TYPE + "=? or "
            + MediaStore.Images.Media.MIME_TYPE + "=?")
    val selectionArgs = arrayOf("image/jpeg", "image/png", "image/jpg")
    val sortOrder = "${MediaStore.Images.Media.DATE_MODIFIED} DESC"
    val localPhotos = arrayListOf<Uri>()
    contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            sortOrder
    )?.use { cursor ->
        while (cursor.moveToNext()) {
            val index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
            val id = cursor.getLong(index)
            val contentUri = ContentUris.withAppendedId(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    id
            )
            localPhotos.add(contentUri)
        }
    }

    return localPhotos
}