android调用系统相机拍照上传,Android 调用系统相机拍照

public class MainActivity extends Activity {

private Button btn_bySysCamera, btn_bySysVideoCamera;

ImageView iv_CameraImg;

private Uri mCameraUri;

File photoFile = null;

String storagePath;

String authority = "com.example.myapplication.fileprovider";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn_bySysCamera = (Button) findViewById(R.id.btn_bySysCamera);

btn_bySysVideoCamera = (Button) findViewById(R.id.btn_bySysVideoCamera);

iv_CameraImg = (ImageView) findViewById(R.id.iv_CameraImg);

btn_bySysCamera.setOnClickListener(click);

btn_bySysVideoCamera.setOnClickListener(click);

}

private View.OnClickListener click = new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent captureIntent = null;

switch (v.getId()) {

case R.id.btn_bySysCamera:

captureIntent = new Intent();

// // 指定开启系统相机的Action

captureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

captureIntent.addCategory(Intent.CATEGORY_DEFAULT);

// 根据文件地址创建文件

Uri photoUri = null;

// if (isAndroidQ) {

// photoUri = createImageUri(); // 适配android 10

// } else {

try {

photoFile = getFileFromCamera();

} catch (Exception e) {

e.printStackTrace();

}

if (photoFile != null) {

mCameraImagePath = photoFile.getAbsolutePath();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//申请临时读取的权限

//适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri

photoUri = FileProvider.getUriForFile(MainActivity.this, authority, photoFile);

} else {

photoUri = Uri.fromFile(photoFile);

}

}

// }

mCameraUri = photoUri;

if (photoUri != null) {

captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);//设置拍照后图片保存的位置

captureIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 设置图片保存的格式

captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

}

startActivityForResult(captureIntent, 1);

break;

// case R.id.btn_bySysVideoCamera:

// intent = new Intent(MainActivity.this, SysVideoCameraActivity.class);

// startActivity(intent);

// break;

default:

break;

}

}

};

/**

* 用于保存图片的文件路径,Android 10以下使用图片路径访问图片

*/

private String mCameraImagePath;

private File getFileFromCamera() {

File imageFile = null;

File storageDir;

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

try {

storagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();

storageDir = new File(storagePath);

storageDir.mkdirs();

imageFile = File.createTempFile(timeStamp, ".jpg", storageDir);

} catch (IOException e) {

e.printStackTrace();

}

return imageFile;

}

Bitmap bitmap;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {

if (resultCode == Activity.RESULT_OK) {//点击确认

if (photoFile != null && photoFile.exists()) {

try {

bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), mCameraUri);//Uri转bitmap

MainActivity.this.runOnUiThread(new Runnable() {

public void run() {

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);//压缩

//把文件插入到系统图库

MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), bitmap, "Title", null);

Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mCameraUri);

MainActivity.this.sendBroadcast(localIntent);

}

});

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

}

清单文件

xml布局文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值