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

发布于 2024-07-14 04:54:55 字数 96 浏览 4 评论 0原文

就像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?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倒带 2024-07-21 04:54:55

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

幸运的是,Android 包含一个用于拾取东西的 Action:Intent.ACTION__PICK 和一个包含图片的数据源:
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI 用于本地设备上的图像或
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI 用于 SD 卡上的图像。

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

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

然后重写 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
    } 
}

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

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

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.

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);

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.

影子是时光的心 2024-07-21 04:54:55

您还可以执行以下操作:

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

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

You can also do:

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

This will pick images across all storages.

过潦 2024-07-21 04:54:55

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

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);
}

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文