Androidは最近の写真やスクリーンショットを入手
11314 ワード
タイプの微信のように、写真を撮ったり、画面を切ったりしてチャットのプラス番号をクリックすると、その画像を送るかどうかを尋ねる弾枠があります.ここではその最新画像を取得するコードを作っただけで、時間間隔を判断していません.
Kotlin版
Java版
Kotlin版
/**
* Created by JokAr on 2017/10/16.
*/
object ImageUtil {
/**
*
*/
fun getLatestPhoto(context: Context): Pair? {
//
val CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"
//
val SCREENSHOTS_IMAGE_BUCKET_NAME = getScreenshotsPath()
// ID
val CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME)
// ID
val SCREENSHOTS_IMAGE_BUCKET_ID = getBucketId(SCREENSHOTS_IMAGE_BUCKET_NAME)
//
val projection = arrayOf(MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_MODIFIED)
val selection = MediaStore.Images.Media.BUCKET_ID + " = ?"
val selectionArgs = arrayOf(CAMERA_IMAGE_BUCKET_ID)
val selectionArgsForScreenshots = arrayOf(SCREENSHOTS_IMAGE_BUCKET_ID)
var cameraPair: Pair? = null
// camera ,
var cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC")
if (cursor.moveToFirst()) {
cameraPair = Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))
}
// Screenshots
var screenshotsPair: Pair? = null
//
cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgsForScreenshots,
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC")
if (cursor.moveToFirst()) {
screenshotsPair = Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))
}
if (!cursor.isClosed) {
cursor.close()
}
//
if (cameraPair != null && screenshotsPair != null) {
return if (cameraPair.first!! > screenshotsPair.first!!) {
screenshotsPair = null
cameraPair
} else {
cameraPair = null
screenshotsPair
}
} else if (cameraPair != null && screenshotsPair == null) {
return cameraPair
} else if (cameraPair == null && screenshotsPair != null) {
return screenshotsPair
}
return null
}
private fun getBucketId(path: String): String {
return path.toLowerCase().hashCode().toString()
}
/**
*
*/
private fun getScreenshotsPath(): String {
var path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Screenshots"
var file: File? = File(path)
if (!file?.exists()!!) {
path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots"
}
file = null
return path
}
}
Java版
/**
*
*
* @param context
* @return
*/
public static Pair getLatestPhoto(Context context) {
//
String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
//
String SCREENSHOTS_IMAGE_BUCKET_NAME = getScreenshotsPath();
// ID
String CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);
// ID
String SCREENSHOTS_IMAGE_BUCKET_ID = getBucketId(SCREENSHOTS_IMAGE_BUCKET_NAME);
//
String[] projection = {MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_MODIFIED};
//
String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
//
String[] selectionArgs = {CAMERA_IMAGE_BUCKET_ID};
String[] selectionArgsForScreenshots = {SCREENSHOTS_IMAGE_BUCKET_ID};
// camera ,
Pair cameraPair = null;
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
if (cursor.moveToFirst()) {
cameraPair = new Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
}
// Screenshots
Pair screenshotsPair = null;
//
cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgsForScreenshots,
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
if (cursor.moveToFirst()) {
screenshotsPair = new Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
}
if (!cursor.isClosed()) {
cursor.close();
}
//
if (cameraPair != null && screenshotsPair != null) {
if (cameraPair.first > screenshotsPair.first) {
screenshotsPair = null;
return cameraPair;
} else {
cameraPair = null;
return screenshotsPair;
}
} else if (cameraPair != null && screenshotsPair == null) {
return cameraPair;
} else if (cameraPair == null && screenshotsPair != null) {
return screenshotsPair;
}
return null;
}
private static String getBucketId(String path) {
return String.valueOf(path.toLowerCase().hashCode());
}
/**
*
* @return
*/
public static String getScreenshotsPath(){
String path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Screenshots";
File file = new File(path);
if(!file.exists()){
path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots";
}
file = null;
return path;
}