如何从画廊获取图像并将其显示在Android SDK中屏幕 [英] How to get images from gallery and display them to the screen in android sdk

查看:140
本文介绍了如何从画廊获取图像并将其显示在Android SDK中屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从图库中pre保存的图像,然后将其显示在屏幕上。任何教程/有用的链接和信息将是AP preciated。如果有什么你想我解释更多,请咨询。


解决方案

 意图photoPickerIntent =新意图(Intent.ACTION_PICK);
photoPickerIntent.setType(图像/ *);
startActivityForResult(photoPickerIntent,1);

这意图用来从你的SD卡挑图像,并使用的onActivityResult()为获取图像,并显示在 ImageView的形象。

 公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
开关(要求code){
情况1:
 {
  如果(结果code == RESULT_OK)
  {
    乌里photoUri = data.getData();
    如果(photoUri!= NULL)
    {
    尝试{
          的String [] = filePathColumn {MediaStore.Images.Media.DATA};
          光标光标= getContentResolver()查询(photoUri,filePathColumn,NULL,NULL,NULL);
     cursor.moveToFirst();
 INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
 字符串文件路径= cursor.getString(参数:columnIndex);
 cursor.close();
 位图BMAP = BitmapFactory.de codeFILE(文件路径);
 image.setImageBitmap(BMAP); }赶上(例外五)
  {}
  }
}
}
}

现在,我们从画廊得到chossed图像,然后将图像ImageVIew.Here image.setImageBitmap(BMAP); 图像设置的ImageView

I would like to know how to get a pre-saved image from the gallery and then display it onto the screen. Any tutorials/helpful links and info would be appreciated. If there is anything you would like me to explain more, please ask.

解决方案

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

This Intent used to pick the images from your SD cards and use onActivityResult() for get image and display the image in ImageView.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
 {
  if (resultCode == RESULT_OK)
  {
    Uri photoUri = data.getData();
    if (photoUri != null)
    {
    try {
          String[] filePathColumn = {MediaStore.Images.Media.DATA};
          Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
     cursor.moveToFirst();
 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
 String filePath = cursor.getString(columnIndex);
 cursor.close();
 Bitmap bMap = BitmapFactory.decodeFile(filePath);
 image.setImageBitmap(bMap);

 }catch(Exception e)
  {}
  }
}
}
}

now we get chossed image from gallery and then set image to ImageVIew.Here image.setImageBitmap(bMap); set the image to ImageView.

这篇关于如何从画廊获取图像并将其显示在Android SDK中屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆