在此调查中我要实现的是:点击Pictures按钮后,获取手机内所有图片,选择某一个图片,并显示到ImageView中。
应用范围: 图片上传时的图片选择 , 类似"浏览"。
所有的图片都会列出来,包括目录。
在Activity Action里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据,并返回该数据的URI.我们利用该常量,然后设置类型为“image/*”,就可获得Android手机内的所有image。
<span style="font-size:18px;">public class Lesson_01_Pic extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
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;
}
}
@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);
}
来源:oschina
链接:https://my.oschina.net/u/1757911/blog/369046