Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取用户拍摄的最后一张照片_Android_Image_Android Contentprovider - Fatal编程技术网

Android 获取用户拍摄的最后一张照片

Android 获取用户拍摄的最后一张照片,android,image,android-contentprovider,Android,Image,Android Contentprovider,嘿,我想通过任何相机应用程序获得用户拍摄的最后一张照片。 我不知道该怎么做 有人能帮我吗 此外,我想将该图像作为电子邮件或彩信的附件发送 谢谢 我还在做彩信发送部分。灵感来自 因此,答案中的主要问题不是所有设备都使用“DCIM”作为相机文件夹。然后我发现,如果一个文件位于应用程序指定的文件夹中,它将被ContentResolver索引,但另一个应用程序无法访问它,这意味着canRead=false。因此,我提出了另一个解决方案: while (cursor.moveToNext()) {

嘿,我想通过任何相机应用程序获得用户拍摄的最后一张照片。 我不知道该怎么做

有人能帮我吗

此外,我想将该图像作为电子邮件或彩信的附件发送

谢谢

我还在做彩信发送部分。

灵感来自

因此,答案中的主要问题不是所有设备都使用“DCIM”作为相机文件夹。然后我发现,如果一个文件位于应用程序指定的文件夹中,它将被
ContentResolver
索引,但另一个应用程序无法访问它,这意味着
canRead=false
。因此,我提出了另一个解决方案:

    while (cursor.moveToNext()) {
        String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
        File imageFile = new File(imagePath);
        if (imageFile.canRead() && imageFile.exists()) {
           // we have found the latest picture in the public folder, do whatever you want
            break;
        }
    }

这是在科特林。我曾经将最后一幅图像加载到imageview中

private fun getLastImageFromGallery(){
        val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

        val projection = arrayOf(
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.Media._ID,
            MediaStore.Images.ImageColumns.DATE_ADDED,
            MediaStore.Images.ImageColumns.MIME_TYPE
        )
        val cursor: Cursor = applicationContext.contentResolver.query(uriExternal, projection, null,
            null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC"
        )!!

        Log.i("Cursor Last", cursor.moveToLast().toString())
        if (cursor.moveToFirst()) {
            val columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
            val imageId: Long = cursor.getLong(columnIndexID)
            val imageURI = Uri.withAppendedPath(uriExternal, "" + imageId)

            Glide.with(applicationContext)
                .load(imageURI)
                .transform(CenterCrop(), RoundedCorners(12))
                .into(imageView)

        }

        cursor.close()
} 

我知道这不是你想问的,但也许这更符合你的意思?您可以启动照相机活动并获取用户拍摄的照片。请看这里@blackjack这是MediaStore.Images.ImageColumns.DATE_TAKEN@lunakid更新答案比记下答案已被弃用要好。:)@Shanerrighton Young,没错。我认为,与其什么也不做,不如留下一个快速的警告,因为有时候这是一个人所能承受的。无论如何,谢谢你努力做“比更好更好”的事情+1美元。干杯我认为这不会带来最后一张拍摄的照片。这将带来截止拍摄日期的最后一张图像。可能是whatsapp图像或instagram图像。也可能尝试按
MediaStore.Images.ImageColumns.\u ID+“DESC”
private fun getLastImageFromGallery(){
        val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

        val projection = arrayOf(
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.Media._ID,
            MediaStore.Images.ImageColumns.DATE_ADDED,
            MediaStore.Images.ImageColumns.MIME_TYPE
        )
        val cursor: Cursor = applicationContext.contentResolver.query(uriExternal, projection, null,
            null, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC"
        )!!

        Log.i("Cursor Last", cursor.moveToLast().toString())
        if (cursor.moveToFirst()) {
            val columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
            val imageId: Long = cursor.getLong(columnIndexID)
            val imageURI = Uri.withAppendedPath(uriExternal, "" + imageId)

            Glide.with(applicationContext)
                .load(imageURI)
                .transform(CenterCrop(), RoundedCorners(12))
                .into(imageView)

        }

        cursor.close()
}