最近一直抽空在做仿微信朋友圈的功能,在點擊加號選擇選擇拍攝進入相機拍照並將圖片顯示出來,因爲版本迭代(Android11馬上也就出來了),所以需要進行版本適配,所以在這裏進行記錄一下,方便以後查看和小夥伴們參考
先來一波效果圖:
微信的效果:
我實現的demo效果:
ok,直接上代碼
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnSavePhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照存儲後顯示" />
<ImageView
android:id="@+id/ivShowPicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp" />
</RelativeLayout>
佈局就是一個簡單的按鈕以及一個圖片,當我們點擊按鈕進入系統相冊中,點擊拍照,然後在onActivityResult()方法回調中對圖片進行處理,讓其顯示在界面上
主要代碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnSavePhoto;
private ImageView ivShowPicture;
private static int REQUEST_CAMERA_1 = 1;
private static int REQUEST_CAMERA_2 = 2;
private String mFilePath;
private String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
init();
// 控件綁定點擊事件
bindClick();
}
// 初始化控件和變量
private void init() {
btnSavePhoto = (Button) findViewById(R.id.btnSavePhoto);
ivShowPicture = (ImageView) findViewById(R.id.ivShowPicture);
}
// 控件綁定點擊事件
private void bindClick() {
btnSavePhoto.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSavePhoto:
// 拍照後存儲並顯示圖片
openCamera_2();
break;
default:
break;
}
}
// 拍照後存儲並顯示圖片
private void openCamera_2() {
File fileDir = new File(Environment.getExternalStorageDirectory(),"Pictures");
if (!fileDir.exists()) {
fileDir.mkdir();
}
fileName = "IMG_" + System.currentTimeMillis() + ".jpg";
mFilePath = fileDir.getAbsolutePath()+"/"+ fileName;
Uri uri = null;
ContentValues contentValues = new ContentValues();
//設置文件名
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/Pictures");
}else {
contentValues.put(MediaStore.Images.Media.DATA, mFilePath);
}
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/JPEG");
uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 啓動系統相機
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQUEST_CAMERA_2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // 如果返回數據
if (requestCode == REQUEST_CAMERA_2) {
try {
//查詢的條件語句
String selection = MediaStore.Images.Media.DISPLAY_NAME + "=? ";
//查詢的sql
//Uri:指向外部存儲Uri
//projection:查詢那些結果
//selection:查詢的where條件
//sortOrder:排序
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID},selection,new String[]{fileName},null);
if (cursor != null && cursor.moveToFirst()) {
do {
Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(0));
Log.i("luingssd","@"+uri);
InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ivShowPicture.setImageBitmap(bitmap);// 顯示圖片
}while (cursor.moveToNext());
}else {
Toast.makeText(this,"no photo",Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
歐克,以上就是完整的代碼了 你可以直接複製進行使用 AndroidQ版本一下別忘記加讀寫權限哦以及權限是否授權判斷,我這裏是demo所以就沒寫
好的,詳細的解釋我就不多說了,有空的小夥伴可以多看一看官方文檔
當然還有最最最最最重要的就是在今天這個特殊的日子裏