您的位置:首页 > 移动开发 > Android开发

Android开发 调用系统相机和加载图片到本地的使用

2016-04-03 20:32 501 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shuhaiyilian/article/details/51051877

/**

*hhl

*

*/


public class TakePhotoActivity extends Activity{

private ImageView mImageView;
private TextView mTakePhoto,mRealPath,mCompressPath;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
initView();
}

/**
* 初始化view
*/
private void initView(){
mImageView=(ImageView)findViewById(R.id.iv_take_photo);
mTakePhoto=(TextView)findViewById(R.id.tv_take_photo);
mRealPath=(TextView)findViewById(R.id.tv_realpath);
mCompressPath=(TextView)findViewById(R.id.tv_compresspaath);

mTakePhoto.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//打开相机
//先验证一下手机是否有sdcard
//获取外部存储状态
String status=Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {//sd卡可用
try {
File dir=new File(Environment.getExternalStorageDirectory()+"/"+"Image/");
if (!dir.exists()) {
//新建一个文件夹
dir.mkdirs();
}
//跳转到系统相机
Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File f=new File(dir,"a.jpg");
//根据文件获取到uri
Uri u=Uri.fromFile(f);
intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
startActivityForResult(intent, 101);
} catch (ActivityNotFoundException e) {
Toast.makeText(TakePhotoActivity.this, "没有找到存储目录", Toast.LENGTH_SHORT);
e.printStackTrace();
}
}else{
Toast.makeText(TakePhotoActivity.this, "SD卡不可用", Toast.LENGTH_SHORT);
}
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 101:
//获取拍完的照片的真是路径
File f=new File(Environment.getExternalStorageDirectory()
+"/"+"Image"+"/"+"a.jpg");
try {
//获取到图片的uri
Uri u=Uri.parse(android.provider.MediaStore.
Images.Media.insertImage(getContentResolver(),
f.getAbsolutePath(), null, null));
//通过uri获取到图片的真实路径
String path=getRealPathFromURI(u);
//把真实路径显示在textview上边
mRealPath.setText(path);
//压缩图片
Bitmap bitmap2=BitmapCompressor.getImage(path);
//返回压缩后的图片的路径
String path2=BitmapCompressor.bitmap2File(bitmap2);
mCompressPath.setText(path2);
//picasso加载本地图片到imageview
Picasso.with(this).load(new File(path2))
.resize(600, 800)//图片尺寸
.centerCrop()//填充类型
.placeholder(R.drawable.logo)//默认图片
.error(R.drawable.logo)//加载出现错误时的默认图片
.into(mImageView);//把图片显示在imageview
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;


default:
break;
}
}

/**
* 从Uri获取到图片的真实路径
* @param contentUri
* @return
*/
public String getRealPathFromURI(Uri contentUri){
String res=null;
String[] proj={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(
contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index=cursor.getColumnIndexOrThrow(
MediaStore.Images.Media.DATA);
res=cursor.getString(column_index);
}
cursor.close();
return res;
}


}

以下是xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEDED"
    android:orientation="vertical" >
    
    <!-- 显示选择的照片 -->
    <ImageView 
        android:id="@+id/iv_take_photo"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/logo"/>
    
    <!-- 打开相机的按钮 -->
    <TextView 
        android:id="@+id/tv_take_photo"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_margin="20dp"
        android:background="@color/orange"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:text="拍照"
        />
    
    <TextView 
        android:id="@+id/tv_realpath"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:background="@color/white"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:text="照片真实路径"
    4000     />
    
    <TextView 
        android:id="@+id/tv_compresspaath"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_margin="5dp"
        android:background="@color/white"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:text="压缩后的路径"
        />
    


</LinearLayout>

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