解决不同类型手机系统照相机拍照无法确定返回问题
添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
activity文件:
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView iv;
private String avatarPath;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.imageView1);
iv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//先创建一个打开照相机功能的intent
File dir=new File(Environment.getExternalStorageDirectory(),"myphoto");
if(!dir.exists()){
dir.mkdirs();
}
file=new File(dir,System.currentTimeMillis()+".jpg");
Uri uri=Uri.fromFile(file);
Intent photoIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoIntent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
photoIntent.putExtra("type",1);
avatarPath=file.getAbsolutePath();
//创建一个打开图库的intent
Intent storeIntent =new Intent(Intent.ACTION_PICK);
storeIntent.setType("image/*");
storeIntent.putExtra("type",0);
storeIntent.setDataAndType(Images.Media.EXTERNAL_CONTENT_URI, "image/*");
//创建intent chooser
Intent chooser=Intent.createChooser(photoIntent,"请选择");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Intent[]{storeIntent});
//发送chooser
startActivityForResult(chooser,101);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK&&requestCode==101){
if(data==null){
//拍照选择的头像文件
takePhoto();
}else if(data.getData().toString().contains("content:")){
//图库选择的头像文件
//是图像在图库中的地址
Uri uri=data.getData();
ContentResolver cr=getContentResolver();
Cursor c= cr.query(uri, null, null, null, null);
c.moveToNext();
//该图像在sd卡上地址
avatarPath=c.getString(c.getColumnIndex(Media.DATA));
c.close();
iv.setImageURI(uri);
}else if(data.getData().toString().contains("file:")){
//拍照选择的头像文件 (部分手机的照相机功能的intent里的data也是有数据的)
takePhoto();
}
}
}
private void takePhoto() {
Options opts=new Options();
//获取实际图片大小的信息,并没有分配空间
opts.inJustDecodeBounds=true;
Bitmap bitmap = BitmapFactory.decodeFile(avatarPath,opts);
opts.inJustDecodeBounds=false;
int w=opts.outWidth;
int h=opts.outHeight;
//自定义宽高
float ww=300f;
float hh=300f;
int be = 0;
if(w>h){
be=(int)(w/ww);
}else{
be=(int)(h/hh);
}
if(be<0)
be=1;
opts.inSampleSize=be;
bitmap = BitmapFactory.decodeFile(avatarPath,opts);
iv.setImageBitmap(bitmap);
updatePictrue();
}
private void updatePictrue() {
//更新图库
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
sendBroadcast(intent);
}
}