Android 10:使用带有位置信息的MediaStore获取图库
创始人
2024-08-12 16:30:16
0

在Android 10中,由于权限限制的变化,使用带有位置信息的MediaStore获取图库的方式发生了变化。以下是一个解决方法的示例代码:

首先,确保你的应用已经获取了READ_EXTERNAL_STORAGEACCESS_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对象并打印每个图片的相关信息。

请注意,上述代码仅仅是一个示例,你可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...