Last active
August 27, 2020 08:47
-
-
Save shuanghua/8d600c9550b91834b4915935d3648e0b to your computer and use it in GitHub Desktop.
调用相机拍照
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application | |
<!--适配7.0+拍照返回 Start--> | |
<provider | |
android:name="android.support.v4.content.FileProvider" | |
android:authorities="${applicationId}.fileProvider" | |
android:exported="false" | |
android:grantUriPermissions="true"> | |
<meta-data | |
android:name="android.support.FILE_PROVIDER_PATHS" | |
android:resource="@xml/provider_path" /> | |
</provider> | |
<!--适配7.0+拍照返回 End--> | |
</application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation "org.permissionsdispatcher:permissionsdispatcher:4.7.0" | |
annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:4.7.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.shuanghua.takephoto_sample; | |
import android.Manifest; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Matrix; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.content.FileProvider; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import permissions.dispatcher.NeedsPermission; | |
import permissions.dispatcher.RuntimePermissions; | |
@RuntimePermissions //第一步 | |
public class MainActivity extends AppCompatActivity { | |
private static final int SCREEN_WIDTH = 120; | |
public static final int REQUEST_TAKE_PHOTO_CAMERA = 1; | |
public static final int REQUEST_TAKE_PHOTO_GALLERY = 2; | |
private ImageView mImageView; | |
private Button mButton; | |
File imgFile; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mImageView = findViewById(R.id.imageView); | |
mButton = findViewById(R.id.button); | |
mButton.setOnClickListener(view -> { | |
MainActivityPermissionsDispatcher.takePhotoWithPermissionCheck(this); // 第四步 | |
// pickPhotoFromGallery(); | |
}); | |
} | |
@NeedsPermission(Manifest.permission.CAMERA) //第二步 | |
public void takePhoto() { | |
Uri imgUri; | |
imgFile = new File(getExternalCacheDir(), "output_image.jpg");// getExternalCacheDir 返回当前应用包名下的 cache 目录路径 | |
if (imgFile.exists()) { | |
imgFile.delete(); | |
} else { | |
try { | |
imgFile.createNewFile(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (Build.VERSION.SDK_INT < 24) { | |
imgUri = Uri.fromFile(imgFile); | |
} else { | |
imgUri = FileProvider.getUriForFile(this, getPackageName() + ".fileProvider", imgFile);//.fileProvider 也可以改成别的,只是一个标识而已 | |
} | |
// uri=content://com.shuanghua.takephoto_sample.fileProvider/external_files/Android/data/com.shuanghua.takephoto_sample/cache/output_image.jpg | |
// file=/storage/emulated/0/Android/data/com.shuanghua.takephoto_sample/cache/output_image.jpg | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri); // 如果需要拍照的原图 (不需要则将该行注释掉,然后在 ActivityResult 通过 data 获取) | |
startActivityForResult(intent, REQUEST_TAKE_PHOTO_CAMERA);// 打开系统相机,同时设置一个 ActivityResult 标记(说明是是从相机页面返回的结果) | |
} | |
private void pickPhotoFromGallery() { | |
Intent intent = new Intent(); | |
intent.setAction(Intent.ACTION_PICK); | |
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(intent, REQUEST_TAKE_PHOTO_GALLERY); | |
} | |
/** | |
* 采样压缩 | |
*/ | |
private Bitmap optionsImg(Uri imgUri) throws FileNotFoundException { | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri), null, options); | |
int width_tmp = options.outWidth, height_tmp = options.outHeight; | |
int scale = 2; | |
while (true) { | |
if (width_tmp / scale < SCREEN_WIDTH) break; | |
scale += 2; | |
} | |
scale /= 2; | |
BitmapFactory.Options options2 = new BitmapFactory.Options(); | |
options2.inSampleSize = scale; | |
return BitmapFactory.decodeStream( | |
getContentResolver().openInputStream(imgUri), null, options2 | |
); | |
} | |
/** | |
* 矩阵压缩 | |
*/ | |
private Bitmap matrixImg(Uri imgUri) throws FileNotFoundException { | |
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri)); | |
System.out.println("缩放前:" + bitmap.getByteCount()); | |
Matrix matrix = new Matrix(); | |
matrix.setScale(0.25f, 0.25f); | |
Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() | |
, bitmap.getHeight(), matrix, true); | |
System.out.println("缩放后:" + bm.getByteCount()); | |
return bm; | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (resultCode == RESULT_OK) { | |
switch (requestCode) { | |
case REQUEST_TAKE_PHOTO_CAMERA: | |
try { | |
Uri imgUri = Uri.fromFile(imgFile); // File to Uri | |
Bitmap bitmap = matrixImg(imgUri); | |
//Bitmap photo = data.getParcelableExtra("data");// 获取系统压缩过后的图片(压缩后的图片特别小, 大概 2cm X 3cm) | |
mImageView.setImageBitmap(bitmap); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
break; | |
case REQUEST_TAKE_PHOTO_GALLERY: | |
try { | |
Uri imgUri = data.getData();// 相册页面返回对应图片的 uri | |
Bitmap bitmap = matrixImg(imgUri); | |
mImageView.setImageBitmap(bitmap); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
break; | |
} | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode | |
, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults); //第三步 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<paths> | |
<external-path | |
name="external_files" | |
path="." /> | |
</paths> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment