在此調查中我要實現的是:點擊Pictures按鈕後,獲取手機內全部圖片,選擇某一個圖片,並顯示到ImageView中。php
應用範圍: 圖片上傳時的圖片選擇 , 相似"瀏覽"。ide
全部的圖片都會列出來,包括目錄。ui
在Activity Action裏面有一個「ACTION_GET_CONTENT」字符串常量,該常量讓用戶選擇特定類型的數據,並返回該數據的URI.咱們利用該常量,而後設置類型爲「image/*」,就可得到Android手機內的全部image。this
view plainspa
<span style="font-size:18px;">public class Lesson_01_Pic extends Activity { .net
/** Called when the activity is first created. */ code
@Override orm
public void onCreate(Bundle savedInstanceState) { blog
super.onCreate(savedInstanceState); 圖片
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.b01);
button.setText("選擇圖片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 開啓Pictures畫面Type設定爲image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT這個Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片後返回本畫面 */
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 將Bitmap設定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
</span>
好了,就將這麼多。
下面是能夠獲取當前拍照的方法:
<span style="font-size:18px;">new AlertDialog.Builder(EditInfoActivity.this)
.setTitle(R.string.update_select_dialog)
.setItems(R.array.update_user_icon,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
/* User clicked so do some stuff */
switch (which) {
case 0:
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, REQ_CODE_CAMERA);
break;
case 1:
Intent intent = new Intent();
intent.setData(Uri
.parse("content://media/internal/images/media"));
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent
.createChooser(intent,
"Select Picture"),
REQ_CODE_PICTURE);
break;
}
}
}).create().show();
/* 0 --> 手機拍照;
1 --> 手機相冊 */</span>
<span style="font-size:18px;">@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQ_CODE_CAMERA:
Bundle bundle = data.getExtras();
// Uri camareUri = (Uri) bundle.get(MediaStore.EXTRA_OUTPUT);
Log.i("Camre", data.getDataString());
Bitmap camerabmp = (Bitmap) data.getExtras().get("data");
ivIcon.setImageBitmap(camerabmp);
break;
case REQ_CODE_PICTURE:
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
cursor.moveToFirst();
try {
srcpath = cursor.getString(1);
Log.i("OnActivtyResult",
"File path :[" + cursor.getColumnCount() + srcpath
+ "]");
InputStream is = new FileInputStream(cursor.getString(1));
Bitmap bmp = ImageLoader.createBitmap(is, 1);
ivIcon.setImageBitmap(bmp);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}</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);}