从我的 Android 应用程序中的图片应用程序访问图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/550905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:17:14  来源:igfitidea点击:

Access pictures from Pictures app in my android app

androidandroid-image

提问by lostInTransit

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?

就像 iPhone 有一个 UIImagePickerController 来让用户访问存储在设备上的图片,我们在 Android SDK 中有类似的控件吗?

Thanks.

谢谢。

回答by Reto Meier

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

您可以使用startActivityForResult, 传入描述您想要完成的操作的 Intent 和执行该操作的数据源。

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

幸运的是,Android 包含一个用于挑选事物的操作:Intent.ACTION__PICK和一个包含图片的数据源: android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI用于本地设备android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI上的图像或 SD 卡上的图像。

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

调用startActivityForResult传递选择操作和您希望用户从中选择的图像,如下所示:

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

Then override onActivityResultto listen for the user having made a selection.

然后覆盖onActivityResult以听取用户进行了选择。

@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.

获得图像 Uri 后,您可以使用它来访问图像并执行任何您需要使用的操作。

回答by jeffreyveon

You can also do:

你也可以这样做:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);

This will pick images across all storages.

这将在所有存储中挑选图像。

回答by lokoko

Just an update to the answer given by Reto. You could do this to scale the image :

只是对 Reto 给出的答案的更新。你可以这样做来缩放图像:

private String getPath(Uri uri) {
String[]  data = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(context, uri, data, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}