在Android 10中,由于权限限制的变化,使用带有位置信息的MediaStore获取图库的方式发生了变化。以下是一个解决方法的示例代码:
首先,确保你的应用已经获取了READ_EXTERNAL_STORAGE
和ACCESS_MEDIA_LOCATION
权限。在AndroidManifest.xml文件中添加以下权限声明:
然后,在你的Activity中,使用以下代码来获取带有位置信息的图片列表:
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 检查权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_MEDIA_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_MEDIA_LOCATION}, REQUEST_PERMISSION);
} else {
getImagesWithLocation();
}
} else {
getImagesWithLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getImagesWithLocation();
} else {
Log.d("MainActivity", "Permission denied");
}
}
}
private void getImagesWithLocation() {
Uri collection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
String[] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.LATITUDE,
MediaStore.Images.Media.LONGITUDE
};
String selection = MediaStore.Images.Media.LATITUDE + " IS NOT NULL AND " + MediaStore.Images.Media.LONGITUDE + " IS NOT NULL";
try (Cursor cursor = getContentResolver().query(
collection,
projection,
selection,
null,
null
)) {
if (cursor != null && cursor.moveToFirst()) {
do {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
double latitude = cursor.getDouble(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.LATITUDE));
double longitude = cursor.getDouble(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.LONGITUDE));
Log.d("MainActivity", "Image ID: " + id);
Log.d("MainActivity", "Image Name: " + name);
Log.d("MainActivity", "Image Latitude: " + latitude);
Log.d("MainActivity", "Image Longitude: " + longitude);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在上述代码中,首先检查应用是否已经获取了所需权限。如果权限已经被授予,调用getImagesWithLocation()
方法来获取带有位置信息的图片列表。
在getImagesWithLocation()
方法中,我们首先根据Android版本来选择正确的Uri获取方式。然后定义了需要查询的列和查询条件,然后使用getContentResolver().query()
方法来查询带有位置信息的图片。最后,我们遍历Cursor对象并打印每个图片的相关信息。
请注意,上述代码仅仅是一个示例,你可以根据自己的需求进行修改和扩展。