Activity
package com.jj.day7_videoview.demo1;
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.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import com.jj.day7_videoview.R;
import java.io.File;
public class CamearImageActivity extends AppCompatActivity {
private ImageView iv;
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camear_image);
iv=(ImageView)findViewById(R.id.iv);
path=getExternalCacheDir().getAbsolutePath()+ File.separator+"222.png";
}
public void onClick(View view) {
switch (view.getId()){
case R.id.btn1:
Intent intent=new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,200);
break;
case R.id.btn2:
Intent intent2=new Intent();
intent2.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(intent2,300);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==200&&resultCode== Activity.RESULT_OK){
Bundle bundle=data.getExtras();
Bitmap bm=(Bitmap)bundle.get("data");
if (bm!=null){
iv.setImageBitmap(bm);
}
}else if (requestCode==300&&resultCode==Activity.RESULT_OK){
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(path,options);
int outHeight=options.outHeight;
int outWidth=options.outWidth;
int sampleHeight=outHeight/200;
int sampleWidth=outWidth/200;
int size=Math.max(sampleHeight,sampleWidth);
options.inSampleSize=size;
options.inPreferredConfig=Bitmap.Config.RGB_565;
options.inJustDecodeBounds=false;
Bitmap bitmap=BitmapFactory.decodeFile(path,options);
iv.setImageBitmap(bitmap);
}
}
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_camear_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jj.day7_videoview.demo1.CamearImageActivity">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转照相界面"
android:onClick="onClick"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="跳转照相界面,保存到指定目录中"/>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>