Demo (MediaStore 访问相册图片)https://developer.android.google.cn/training/data-storage?hl=zh-cn
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
/data/data/com.android.providers.media.module/databases/external.db
不同系统版本该文件位于不同目录下,/data/data/com.android.providers.media xxx/external.db
package com.cuncaojin.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
/**
* 外部存储数据库文件:/data/data/com.android.providers.media.module/databases/external.db,
* 不同版本位置略有不同,有的在/data/data/com.android.providers.media/databases/external.db
*
* su root
*
* cd /data/data/com.android.providers.media.module/databases && ls -l
*
* -rw-rw---- 1 u0_a119 u0_a119 131072 2022-03-07 14:23 external.db
* -rw------- 1 u0_a119 u0_a119 32768 2022-03-07 16:31 external.db-shm
* -rw------- 1 u0_a119 u0_a119 477952 2022-03-07 16:31 external.db-wal
* -rw-rw---- 1 u0_a119 u0_a119 233472 2022-03-07 16:31 internal.db
* -rw------- 1 u0_a119 u0_a119 32768 2022-03-07 16:31 internal.db-shm
* -rw------- 1 u0_a119 u0_a119 524288 2022-03-07 16:31 internal.db-wal
*/
public class MainActivity extends AppCompatActivity {
public static final String SIMPLE_IMAGE_NAME = "IMG_20220307_142202.jpg";
public static final String SIMPLE_IMAGE_NAME2 = "IMG_20220307_141816.jpg";
private ImageView iv;
private ImageView iv2;
private Uri mUri;
private Uri mUri2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.imageView);
iv2 = findViewById(R.id.imageView2);
requiresPermissions();
}
public static Uri getImageStreamFromExternal(String imageName) {
File externalPubPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile = new File(externalPubPath, imageName);
// 等价
// File imageFile = new File("/storage/emulated/0/Pictures/IMG_20220307_142202.jpg");
Uri uri = null;
if (imageFile.exists()) {
uri = Uri.fromFile(imageFile);
}
return uri;
}
@SuppressLint("Range")
public static Uri getMediaUriFromPath(Context context, String path) {
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = context.getContentResolver().query(mediaUri,
null,
MediaStore.Images.Media.DISPLAY_NAME + "= ?",
new String[]{path.substring(path.lastIndexOf("/") + 1)},
null);
Uri uri = null;
if (cursor.moveToFirst()) {
uri = ContentUris.withAppendedId(mediaUri,
cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
}
cursor.close();
return uri;
}
public List<Uri> getAllShownImagesPath() {
Uri uriExternal = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cursor;
int columnIndexID;
List<Uri> listOfAllImages = new ArrayList<>();
// /data/data/com.android.providers.media.module/databases/external.db
// SELECT _id,
// _display_name
// _data,
// FROM images;
// 45 /storage/emulated/0/Pictures/IMG_20220307_142051.jpg IMG_20220307_142051.jpg
String[] projection = new String[]{MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA};
Long imageId;
cursor = getContentResolver().query(uriExternal, projection, null, null, null);
if (cursor != null) {
columnIndexID = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int simpleNamePathId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
// 废弃字段,不要用
int fullNamePathId = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
while (cursor.moveToNext()) {
imageId = cursor.getLong(columnIndexID);
String simpleNamePath = cursor.getString(simpleNamePathId);
String fullNamePath = cursor.getString(fullNamePathId);
if (SIMPLE_IMAGE_NAME2.equals(simpleNamePath)) {
mUri2 = ContentUris.withAppendedId(uriExternal, imageId);// 或 Uri uriImage = Uri.withAppendedPath(uriExternal, "" + imageId);
}
Uri uriImage = ContentUris.withAppendedId(uriExternal, imageId);// 或 Uri uriImage = Uri.withAppendedPath(uriExternal, "" + imageId);
listOfAllImages.add(uriImage);
}
cursor.close();
}
return listOfAllImages;
}
private void showAlbumPicture() {
try {
// 左图
mUri = getImageStreamFromExternal(SIMPLE_IMAGE_NAME);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mUri);
iv.setImageBitmap(bitmap);
// 右图
getAllShownImagesPath();
Bitmap bitmap2 = MediaStore.Images.Media.getBitmap(getContentResolver(), mUri2);
iv2.setImageBitmap(bitmap2);
} catch (IOException e) {
e.printStackTrace();
}
}
/// EasyPermissions start /
public static final int EXTERNAL_STORAGE_REQUEST_CODE = 111;
@AfterPermissionGranted(EXTERNAL_STORAGE_REQUEST_CODE)
private void requiresPermissions() {
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
// Already have permission, do the thing
showAlbumPicture();
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this, getString(R.string.app_name),
EXTERNAL_STORAGE_REQUEST_CODE, perms);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
/// EasyPermissions end /
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)