android、获取本地图片|直接获取照相图片

来源:互联网 发布:mk文件编译data数据 编辑:程序博客网 时间:2024/04/26 08:10

在此调查中我要实现的是:点击Pictures按钮后,获取手机内所有图片,选择某一个图片,并显示到ImageView中。

应用范围: 图片上传时的图片选择  , 类似"浏览"。

所有的图片都会列出来,包括目录。

在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,然后设置类型为“image/*”,就可获得Android手机内的所有image。

 


view plainprint?
  1. <span style="font-size:18px;">public class Lesson_01_Pic extends Activity {    
  2.     /** Called when the activity is first created. */    
  3.     @Override    
  4.     public void onCreate(Bundle savedInstanceState) {    
  5.         super.onCreate(savedInstanceState);    
  6.         setContentView(R.layout.main);    
  7.             
  8.         Button button = (Button)findViewById(R.id.b01);    
  9.         button.setText("选择图片");    
  10.         button.setOnClickListener(new Button.OnClickListener(){    
  11.             @Override    
  12.             public void onClick(View v) {    
  13.                 Intent intent = new Intent();    
  14.                 /* 开启Pictures画面Type设定为image */    
  15.                 intent.setType("image/*");    
  16.                 /* 使用Intent.ACTION_GET_CONTENT这个Action */    
  17.                 intent.setAction(Intent.ACTION_GET_CONTENT);     
  18.                 /* 取得相片后返回本画面 */    
  19.                 startActivityForResult(intent, 1);    
  20.             }    
  21.                 
  22.         });    
  23.     }    
  24.         
  25.     @Override    
  26.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
  27.         if (resultCode == RESULT_OK) {    
  28.             Uri uri = data.getData();    
  29.             Log.e("uri", uri.toString());    
  30.             ContentResolver cr = this.getContentResolver();    
  31.             try {    
  32.                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));    
  33.                 ImageView imageView = (ImageView) findViewById(R.id.iv01);    
  34.                 /* 将Bitmap设定到ImageView */    
  35.                 imageView.setImageBitmap(bitmap);    
  36.             } catch (FileNotFoundException e) {    
  37.                 Log.e("Exception", e.getMessage(),e);    
  38.             }    
  39.         }    
  40.         super.onActivityResult(requestCode, resultCode, data);    
  41.     }    
  42. }    
  43. </span>  

 

 

好了,就将这么多。


下面是可以获取当前拍照的方法:

view plainprint?
  1. <span style="font-size:18px;">new AlertDialog.Builder(EditInfoActivity.this)  
  2.                 .setTitle(R.string.update_select_dialog)  
  3.                 .setItems(R.array.update_user_icon,  
  4.                         new DialogInterface.OnClickListener() {  
  5.                             public void onClick(DialogInterface dialog,  
  6.                                     int which) {  
  7.                                 /* User clicked so do some stuff */  
  8.                                 switch (which) {  
  9.                                 case 0:  
  10.                                     Intent i = new Intent(  
  11.                                             MediaStore.ACTION_IMAGE_CAPTURE);  
  12.                                     startActivityForResult(i, REQ_CODE_CAMERA);  
  13.                                     break;  
  14.                                 case 1:  
  15.                                     Intent intent = new Intent();  
  16.                                     intent.setData(Uri  
  17.                                             .parse("content://media/internal/images/media"));  
  18.                                     intent.setAction(Intent.ACTION_PICK);  
  19.                                     startActivityForResult(Intent  
  20.                                             .createChooser(intent,  
  21.                                                     "Select Picture"),  
  22.                                             REQ_CODE_PICTURE);  
  23.                                     break;  
  24.                                 }  
  25.                             }  
  26.                         }).create().show();  
  27.         /* 0 --> 手机拍照; 
  28.               1 --> 手机相册 */</span>  

view plainprint?
  1. <span style="font-size:18px;">@Override  
  2.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.         if (resultCode == RESULT_OK) {  
  4.             switch (requestCode) {  
  5.             case REQ_CODE_CAMERA:  
  6.                 Bundle bundle = data.getExtras();  
  7.                 // Uri camareUri = (Uri) bundle.get(MediaStore.EXTRA_OUTPUT);  
  8.                 Log.i("Camre", data.getDataString());  
  9.                 Bitmap camerabmp = (Bitmap) data.getExtras().get("data");  
  10.                 ivIcon.setImageBitmap(camerabmp);  
  11.                 break;  
  12.             case REQ_CODE_PICTURE:  
  13.                 Uri uri = data.getData();  
  14.                 Cursor cursor = getContentResolver().query(uri, nullnull,  
  15.                         nullnull);  
  16.                 cursor.moveToFirst();  
  17.                 try {  
  18.                     srcpath = cursor.getString(1);  
  19.   
  20.                     Log.i("OnActivtyResult",  
  21.                             "File path :[" + cursor.getColumnCount() + srcpath  
  22.                                     + "]");  
  23.                     InputStream is = new FileInputStream(cursor.getString(1));  
  24.                     Bitmap bmp = ImageLoader.createBitmap(is, 1);  
  25.                     ivIcon.setImageBitmap(bmp);  
  26.                 } catch (Exception e) {  
  27.                     e.printStackTrace();  
  28.                 }  
  29.                 break;  
  30.             }  
  31.   
  32.         }  
  33.         super.onActivityResult(requestCode, resultCode, data);  
  34.     }</span>  


关于另一种从相册获取的方法 更实用
private File tempFile;
this.tempFile = new File(FilePathUtility.GetReAudioFilePath(
PersonalEditActivity.this, "head.jpg", true));
case R.id.update:
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 出现截取界面 */
intent.putExtra("crop", "true");
/*保存到SD*/
intent.putExtra("output", Uri.fromFile(tempFile));
/*设置图片像素*/
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
/*设置图片格式*/
intent.putExtra("outputFormat", "JPEG");
/* 设置比例 1:1 */
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
break;


}


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// Uri uri = data.getData();
// Log.e("uri", uri.toString());
// ContentResolver cr = this.getContentResolver();
try {
// Bitmap bitmap =
// BitmapFactory.decodeStream(cr.openInputStream(uri));
// bitmap = ImageMatrixTool.BittoBit(bitmap);
// ImageMatrixTool.savePic(bitmap,
// FilePathUtility.GetReAudioFilePath(PersonalEditActivity.this,
// "head.png", true));


imageView.setImageDrawable(Drawable.createFromPath(tempFile
.getAbsolutePath()));
}
// catch (FileNotFoundException e) {
// Log.e("Exception", e.getMessage(),e);
// }
catch (OutOfMemoryError e) {
Log.e(TAG, "out of memory");
}
}


}


super.onActivityResult(requestCode, resultCode, data);
}