码迷,mamicode.com
首页 > 其他好文 > 详细

017_03获取图库图片

时间:2015-05-31 19:56:52      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

  当需要在之前Activity中得到新打开Activity的数据,需要系统提供的startActivityForResult(Intent intent,int requestCode)方法。为了取得传回的数据,需要在之前的Activity中重写onActivityResult(int requestCode, int resultCode,Intent data)方法。

 1 package com.example.day17_03getExif;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.database.Cursor;
 6 import android.net.Uri;
 7 import android.os.Bundle;
 8 import android.provider.MediaStore;
 9 import android.view.View;
10 import android.widget.ImageView;
11 
12 public class MainActivity extends Activity {
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18     }
19     
20     public void getpicturefromgallery(View v){
21         
22         Intent intent = new Intent();
23         intent.setAction("android.intent.action.PICK");
24         intent.setType("image/*");
25         startActivityForResult(intent, 100);     
26     }
27     
28     @Override
29     protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
30         if (requestCode==100 &&resultCode==RESULT_OK ) {
31             //resultcode ok才可以从里面去获取你要的数据
32             System.out.println("MainActivity.onActivityResult()"+resultCode);
33             ImageView iv_photo = (ImageView) findViewById(R.id.iv_photo);
34             
35             Uri uri =data.getData();
36             System.out.println(uri);
37             iv_photo.setImageURI(uri);
38             
39             System.out.println("MainActivity.onActivityResult()"+uri.toString());        
40             String[] filePathColumn = { MediaStore.Images.Media.DATA };
41             Cursor cursor=    getContentResolver().query(uri, filePathColumn, null, null, null);
42             cursor.moveToNext();    
43             String filepath = cursor.getString(0);
44             System.out.println("MainActivity.onActivityResult()"+filepath);  
45         }    
46     }    
47 }

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.day17_03getExif.MainActivity"
10     android:orientation="vertical" >
11     
12     <Button
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="获取图库图片" 
16         android:onClick="getpicturefromgallery"/>
17     
18     <ImageView
19         android:id="@+id/iv_photo"  
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content" />
22         
23 </LinearLayout>

 

1 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

 

017_03获取图库图片

标签:

原文地址:http://www.cnblogs.com/woodrow2015/p/4542499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!