从图片应用程序访问的照片在我的Andr​​oid应用程序应用程序、我的、照片、图片

2023-09-12 21:36:11 作者:低调做人,高调做事

就像iPhone拥有的UIImagePickerController让存储在设备上的用户访问图片,我们是否在Android SDK中类似的控制?

Just like the iPhone has a UIImagePickerController to let the user access pictures stored on the device, do we have a similar control in the Android SDK?

感谢。

推荐答案

您可以使用 startActivityForResult ,传递描述你想要完成的动作,并与数据源的意图对执行的操作。

You can use startActivityForResult, passing in an Intent that describes an action you want completed and and data source to perform the action on.

幸运的是,Android包括采摘事的行动: Intent.ACTION__PICK 和含图片数据来源: android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI 本地设备上的图像或 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI 在SD卡上的图像。

Luckily for you, Android includes an Action for picking things: Intent.ACTION__PICK and a data source containing pictures: android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI for images on the local device or android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI for images on the SD card.

呼叫 startActivityForResult 传递你希望用户从这样的点击选择动作和图像:

Call startActivityForResult passing in the pick action and the images you want the user to select from like this:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);

然后重写 onActivityResult 监听作出一个选择用户。

Then override onActivityResult to listen for the user having made a selection.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == SELECT_IMAGE)
    if (resultCode == Activity.RESULT_OK) {
      Uri selectedImage = data.getData();
      // TODO Do something with the select image URI
    } 
}

一旦你的形象开放的,你可以用它来访问图像和做任何你需要做的吧。

Once you have the image Uri you can use it to access the image and do whatever you need to do with it.