如何设置从SD卡中选择图像在Android中的ImageView [英] How to set selected image from SD card on imageview in Android

查看:104
本文介绍了如何设置从SD卡中选择图像在Android中的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的初学者。我想开发自己的画廊。但我面临的一个问题。当我去到SD卡在我的记忆和选择照片,然后我试图用我的图片浏览器应用打开图像。但是,当我选择图像查看器,然后另一个意图要求选择我的形象。但是,我想直接显示在的ImageView 选定的图像。请人帮我。

我的code:

 公共类MainActivity延伸活动{     私有静态最终诠释REQUEST_ code = 1;
      私人位图位图;
      私人ImageView的ImageView的;
      私人字符串selectedImagePath;
      @覆盖
      公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        ImageView的=(ImageView的)findViewById(R.id.result);
        pickImage();
      }      公共无效pickImage(){
        意向意图=新的Intent();
        intent.setType(图像/ *);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(意向,REQUEST_ code);
      }      @覆盖
      保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        InputStream的流= NULL;
        如果(要求code == REQUEST_ code和;&安培;结果code == Activity.RESULT_OK)
          尝试{
            // recyle未使用的位图                    乌里selectedImage = data.getData();                    的String [] = filePathColumn {MediaStore.Images.Media.DATA};
                    光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
                    cursor.moveToFirst();
                    INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
                    字符串文件路径= cursor.getString(参数:columnIndex);                    Log.v(罗尼,文件路径);
                    cursor.close();                    如果(位图= NULL&放大器;!&安培;!bitmap.isRecycled())
                    {
                        位= NULL;
                    }                    位= BitmapFactory.de codeFILE(文件路径);
                    imageView.setBackgroundResource(0);
                    imageView.setImageBitmap(位图);
           // imageView.setImageBitmap(位图);
          }赶上(例外五){
            e.printStackTrace();
          } {最后
            如果(流!= NULL)
              尝试{
                stream.close();
              }赶上(IOException异常五){
                e.printStackTrace();
              }
          }
      }
      私人字符串的getPath(URI URI)
      {
      的String [] =投影{MediaStore.Images.Media.DATA};
      光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
      INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      返回cursor.getString(Column_Index中);
  }
}

/////后改变code

 公共类MainActivity延伸活动{ 私有静态最终诠释REQUEST_ code = 1;
  私人位图位图;
  私人ImageView的ImageView的;
  私人字符串selectedImagePath;
  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    ImageView的=(ImageView的)findViewById(R.id.result);    意图I =新意图(
            Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);    startActivityForResult(I,REQUEST_ code);
  }  @覆盖
  保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    InputStream的流= NULL;
    如果(要求code == REQUEST_ code和;&安培;结果code == Activity.RESULT_OK)
      尝试{
        // recyle未使用的位图                乌里selectedImage = data.getData();                的String [] = filePathColumn {MediaStore.Images.Media.DATA};
                光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
                cursor.moveToFirst();
                INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
                字符串文件路径= cursor.getString(参数:columnIndex);                Log.v(罗尼,文件路径);
                cursor.close();                如果(位图= NULL&放大器;!&安培;!bitmap.isRecycled())
                {
                    位= NULL;
                }                位= BitmapFactory.de codeFILE(文件路径);
                //imageView.setBackgroundResource(0);
                imageView.setImageBitmap(位图);
       // imageView.setImageBitmap(位图);
      }赶上(例外五){
        e.printStackTrace();
      } {最后
        如果(流!= NULL)
          尝试{
            stream.close();
          }赶上(IOException异常五){
            e.printStackTrace();
          }
      }
  }
  私人字符串的getPath(URI URI)
  {
  的String [] =投影{MediaStore.Images.Media.DATA};
  光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
  INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  返回cursor.getString(Column_Index中); }}


解决方案

让我们假设你有一个名为活性 ViewActivity 这显示选定的图像。

的Andr​​oidManifest.xml 通过添加此设置你的活动作为一个图像浏览器活动:

 <活动机器人:名字=。ViewActivity>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.VIEW/>
        <类机器人:名字=android.intent.category.DEFAULT/>
        <数据机器人:mime类型=图像/ */>
    &所述; /意图滤光器>
< /活性GT;

的onCreate()做这个 ViewActivity 捕获图像传递方法:

 意向意图= getIntent();
乌里数据= intent.getData();
//检查如果数据类型是图片
如果(intent.getType()的indexOf(图像/)!= -1)
{
    myImageView.setImageURI(数据);
}

I am a beginner of android development. I am trying to develop my own gallery. But I am facing a problem. When I go to SD card on my memory and choose a photo then I am trying to open image using my Image Viewer app. But when I select Image Viewer then another intent is called for choosing my image. But I directly want show the selected image on Imageview. Please anyone help me.

My code:

public class MainActivity extends Activity {

     private static final int REQUEST_CODE = 1;
      private Bitmap bitmap;
      private ImageView imageView;
      private String selectedImagePath;


      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.result);
        pickImage();
      }

      public void pickImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        InputStream stream = null;


        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
          try {
            // recyle unused bitmaps

                    Uri selectedImage = data.getData();



                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String filePath = cursor.getString(columnIndex);

                    Log.v("roni", filePath);
                    cursor.close();

                    if(bitmap != null && !bitmap.isRecycled())
                    {
                        bitmap = null;                
                    }

                    bitmap = BitmapFactory.decodeFile(filePath);
                    imageView.setBackgroundResource(0);
                    imageView.setImageBitmap(bitmap);              


           // imageView.setImageBitmap(bitmap);
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            if (stream != null)
              try {
                stream.close();
              } catch (IOException e) {
                e.printStackTrace();
              }
          }
      }
      private String getPath(Uri uri)
      {    
      String[] projection={MediaStore.Images.Media.DATA}; 
      Cursor cursor=managedQuery(uri,projection,null,null,null);
      int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
  }  
}

/////After changing the code

public class MainActivity extends Activity {

 private static final int REQUEST_CODE = 1;
  private Bitmap bitmap;
  private ImageView imageView;
  private String selectedImagePath;


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.result);

    Intent i = new Intent(
            Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, REQUEST_CODE);


  }



  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    InputStream stream = null;


    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
      try {
        // recyle unused bitmaps

                Uri selectedImage = data.getData();



                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);

                Log.v("roni", filePath);
                cursor.close();

                if(bitmap != null && !bitmap.isRecycled())
                {
                    bitmap = null;                
                }

                bitmap = BitmapFactory.decodeFile(filePath);
                //imageView.setBackgroundResource(0);
                imageView.setImageBitmap(bitmap);              


       // imageView.setImageBitmap(bitmap);
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        if (stream != null)
          try {
            stream.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
      }
  }


  private String getPath(Uri uri)
  {    
  String[] projection={MediaStore.Images.Media.DATA}; 
  Cursor cursor=managedQuery(uri,projection,null,null,null);
  int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
  cursor.moveToFirst(); 
  return cursor.getString(column_index); }  }

解决方案

Let's assume you have an activity called ViewActivity that shows selected Image.

in your AndroidManifest.xml set your activity as a Image Viewer Activity by adding this :

<activity android:name=".ViewActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="image/*"/>
    </intent-filter>
</activity>

in onCreate() method of ViewActivity catch passed Image by doing this :

Intent intent = getIntent();
Uri data = intent.getData();
//Check If data type is Image
if (intent.getType().indexOf("image/") != -1)
{
    myImageView.setImageURI(data);
}

这篇关于如何设置从SD卡中选择图像在Android中的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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