从相机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();
}
}
}
}
}
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();
}
}
}
}
}
相关文章推荐
- Android View中重要的回调方法
- webService学习7:调用天气接口
- C++实现二叉树的前序创建 前序遍历
- Ajax入门介绍
- android 自定义View基础(1)
- Docker Compose的网络设置
- 时间复杂度和空间复杂度
- 时间复杂度和空间复杂度
- Qt下如何修改文件的时间(全平台修改)
- XML约束
- 第十六周项目一 a阅读程序(3)
- XMind
- 软件生命周期
- 关于js中onclick字符串传参问题
- 安装oracle客户端口
- phpstudy https
- 第一个项目(手机管家)总结
- Minimum Depth of Binary Tree
- jqXHR 对象(post完成后再调用函数)
- Qt在windows 平台操作保存execel的表格(通过QAxObject来操作)