您的位置:首页 > 其它

从相机or相册获取图片并显示

2016-06-10 17:20 471 查看
啊这个技术应该算是十分稀松平常了,但是对于小白来说,还是要费一番功夫的。因此在这里贴上我的代码,也是为了以后用到的时候方便找。。。

package com.example.photo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

Button camera, gallery;
ImageView photo;
private static int CAMERA_REQUEST_CODE = 1;
private static int GALLERY_REQUEST_CODE = 2;
private static int SCALE=5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera=(Button)findViewById(R.id.camera);
gallery=(Button)findViewById(R.id.gallery);
photo=(ImageView)findViewById(R.id.photo);
camera.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);

//这里如果不加入访问SD卡权限会报错
// <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Uri imageUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "test.png"));
// 指定照片保存路径(SD卡),test.png为一个临时文件,每次拍照后这个图片都会被替换
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(openCameraIntent, CAMERA_REQUEST_CODE);
}
});

gallery.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Uri imageUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "test.png"));
// 指定照片保存路径(SD卡),test.png为一个临时文件,每次拍照后这个图片都会被替换
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, GALLERY_REQUEST_CODE);

}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//另外,使用保存文件之后再读取而不是直接用data的原因是,这里返回的data是一个缩略图,十分不清晰

if (requestCode == CAMERA_REQUEST_CODE) {// 将保存在本地的图片取出并缩小后显示在界面上
Bitmap bitmap = BitmapFactory.decodeFile(Environment
.getExternalStorageDirectory()
+ "/test.png");
Bitmap newBitmap = ImageTools.zoomBitmap(bitmap, bitmap.getWidth()
/ SCALE, bitmap.getHeight() / SCALE);
// 由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常
bitmap.recycle();
// 将处理过的图片显示在界面上,并保存到本地
ImageView imageView = (ImageView) findViewById(R.id.photo);
imageView.setImageBitmap(newBitmap);

} else if (requestCode == GALLERY_REQUEST_CODE) {
if (data == null)
return;
else {
Uri uri;
uri = data.getData();
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), uri);
ImageView imageView = (ImageView) findViewById(R.id.photo);
imageView.setImageBitmap(bm);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: