布局文件
<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.takephoto.MainActivity">
<Button
android:text="拍照"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:id="@+id/button"
android:onClick="takePhone"
/>
<Button
android:text="相册选择"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:onClick="choosePhone"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/image"
android:src="@color/colorAccent"
android:scaleType="centerCrop"
android:layout_weight="7"/>
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
MainActivity代码
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private static final int CROP_PHOTO = 2;
private static final int REQUEST_CODE_PICK_IMAGE=3;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 6;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE2 = 7;
private File output;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
void initView(){
imageView=(ImageView)findViewById(R.id.image);
}
public void takePhone(View view){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_CALL_PHONE2);
}else {
takePhoto();
}
}
public void choosePhone(View view){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_CALL_PHONE2);
}else {
choosePhoto();
}
}
/**
* 拍照
*/
void takePhoto(){
/**
* 最后一个参数是文件夹的名称,可以随便起
*/
File file=new File(Environment.getExternalStorageDirectory(),"拍照");
if(!file.exists()){
file.mkdir();
}
/**
* 这里将时间作为不同照片的名称
*/
output=new File(file,System.currentTimeMillis()+".jpg");
/**
* 如果该文件夹已经存在,则删除它,否则创建一个
*/
try {
if (output.exists()) {
output.delete();
}
output.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
/**
* 隐式打开拍照的Activity,并且传入CROP_PHOTO常量作为拍照结束后回调的标志
*/
imageUri = Uri.fromFile(output);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
/**
* 从相册选取图片
*/
void choosePhoto(){
/**
* 打开选择图片的界面
*/
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");//相片类型
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
public void onActivityResult(int req, int res, Intent data) {
switch (req) {
/**
* 拍照的请求标志
*/
case CROP_PHOTO:
if (res==RESULT_OK) {
try {
/**
* 该uri就是照片文件夹对应的uri
*/
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bit);
} catch (Exception e) {
Toast.makeText(this,"程序崩溃",Toast.LENGTH_SHORT).show();
}
}
else{
Log.i("tag", "失败");
}
break;
/**
* 从相册中选取图片的请求标志
*/
case REQUEST_CODE_PICK_IMAGE:
if (res == RESULT_OK) {
try {
/**
* 该uri是上一个Activity返回的
*/
Uri uri = data.getData();
Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageView.setImageBitmap(bit);
} catch (Exception e) {
e.printStackTrace();
Log.d("tag",e.getMessage());
Toast.makeText(this,"程序崩溃",Toast.LENGTH_SHORT).show();
}
}
else{
Log.i("liang", "失败");
}
break;
default:
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
takePhoto();
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE2)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
choosePhoto();
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
拍照
最新推荐文章于 2024-02-26 21:30:24 发布