package com.youga.capturingphotos;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final int REQ_THUMB = 222;
private static final int REQ_GALLERY = 333;
private static final String TAG = "MainActivity";
private static final int REQ_TAKE_PHOTO = 444;
Button mThumbnail, mFullSize, mAddGallery;
ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 200);
}
mThumbnail = (Button) findViewById(R.id.thumbnail);
mFullSize = (Button) findViewById(R.id.fullSize);
mAddGallery = (Button) findViewById(R.id.addGallery);
mImageView = (ImageView) findViewById(R.id.imageView);
mThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {//判断是否有相机应用
startActivityForResult(takePictureIntent, REQ_THUMB);
}
}
});
mFullSize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {//判断是否有相机应用
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();//创建临时图片文件
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
//FileProvider 是一个特殊的 ContentProvider 的子类,
//它使用 content:// Uri 代替了 file:/// Uri. ,更便利而且安全的为另一个app分享文件
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
"com.youga.fileprovider",
photoFile);
Log.i(TAG, "photoURI:" + photoURI.toString());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQ_TAKE_PHOTO);
}
}
}
});
mAddGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {//判断是否有相机应用
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createPublicImageFile();//创建临时图片文件
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
"com.youga.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQ_GALLERY);
}
}
galleryAddPic();
}
});
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
//.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//创建临时文件,文件前缀不能少于三个字符,后缀如果为空默认未".tmp"
File image = File.createTempFile(
imageFileName, /* 前缀 */
".jpg", /* 后缀 */
storageDir /* 文件夹 */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
String mPublicPhotoPath;
private File createPublicImageFile() throws IOException {
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
// Create an image file name
Log.i(TAG, "path:" + path.getAbsolutePath());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
String imageFileName = "JPEG_" + timeStamp;
File image = File.createTempFile(
imageFileName, /* 前缀 */
".jpg", /* 后缀 */
path /* 文件夹 */
);
mPublicPhotoPath = image.getAbsolutePath();
return image;
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mPublicPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_THUMB://返回结果
if (resultCode != Activity.RESULT_OK) return;
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
break;
case REQ_TAKE_PHOTO://返回结果
if (resultCode != Activity.RESULT_OK) return;
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurre
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论














收起资源包目录


























































共 27 条
- 1
资源评论

- 生活的艺术4142022-04-03解压后怎么使用啊
- 人生如忐忑2020-08-16下载怎么安装啊

qq_27725169
- 粉丝: 2
- 资源: 6
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
