android、獲取本地圖片|直接獲取照相圖片

在此調查中我要實現的是:點擊Pictures按鈕後,獲取手機內全部圖片,選擇某一個圖片,並顯示到ImageView中。php

應用範圍: 圖片上傳時的圖片選擇  , 相似"瀏覽"。ide

全部的圖片都會列出來,包括目錄。ui

在Activity Action裏面有一個「ACTION_GET_CONTENT」字符串常量,該常量讓用戶選擇特定類型的數據,並返回該數據的URI.咱們利用該常量,而後設置類型爲「image/*」,就可得到Android手機內的全部image。this

 


view plainspa

  1. <span style="font-size:18px;">public class Lesson_01_Pic extends Activity {    .net

  2.     /** Called when the activity is first created. */    code

  3.     @Override    orm

  4.     public void onCreate(Bundle savedInstanceState) {    blog

  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 plain

  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 plain

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

相關文章
相關標籤/搜索