Android中呼叫攝像頭拍照儲存,並在相簿中選擇圖片顯示
我的配置是:
android手機版本是7.0
android studio是3.1
android平臺是9.0
我的測試都是在真機上進行的。
首先來看程式碼:
package com.example.sunshunli.cameraalbumtest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
public static final int TAKE_PHOTO = 1;
private ImageView picture;
private Uri imageUri;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button takephoto = findViewById(R.id.take_photo);
picture = findViewById(R.id.picture);
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 用來存放攝像頭拍下的圖片,把圖片命名為output_image.jpg
File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//第二種方法(推薦使用第二種方法)
// File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
// if (!file.getParentFile().exists())file.getParentFile().mkdirs();
// Uri imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.sunshunli.cameraalbumtest.fileprovider", file);//通過FileProvider建立一個content型別的Uri
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //新增這一句表示對目標應用臨時授權該Uri所代表的檔案
// intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設定Action為拍照
// intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片儲存到指定URI
// startActivityForResult(intent,1006);
if (Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(MainActivity.this,"com.example.sunshunli.cameraalbumtest.fileprovider",outputImage);
} else {
//將File物件轉換成URI物件,這個URI物件標識著output_image.jpg這張圖片的本地真是路徑
imageUri = Uri.fromFile(outputImage);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
Log.d(TAG, "requestCode:"+requestCode);//1
Log.d(TAG, "data: "+data);//intent{}
Log.d(TAG, "resultCode: "+resultCode);//-1
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
startActivityForResult(intent, TAKE_PHOTO);
這是我們用來啟動另一個拍照活動的,這個與startactivity是不一樣的,startactivity只有一個引數,intent,這個有兩個引數,第二個引數是requestcode,請求碼,我們讓請求碼為1.
如果想在Activity中得到新開啟Activity 關閉後返回的資料,需要使用系統提供的startActivityForResult(Intent intent, int requestCode)方法開啟新的Activity,新的Activity 關閉後會向前面的Activity傳回資料,為了得到傳回的資料,必須在前面的Activity中重寫onActivityResult(int requestCode, int resultCode, Intent data)方法。
我們在拍照結束後會有一個resultcode,如果拍照成功的話就會執行下面的操作。
<?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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/take_photo"
android:text="Take photo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/picture"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="my_images"
path=""/>
</paths>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sunshunli.cameraalbumtest">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.sunshunli.cameraalbumtest.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
下面我們介紹一下在相簿中選擇圖片並顯示
package com.example.sunshunli.cameraalbumtest;
import android.Manifest;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
public static final int TAKE_PHOTO = 1;
public static final int CHOOSE_PHOTO = 2;
private ImageView picture;
private Uri imageUri;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button takephoto = findViewById(R.id.take_photo);
Button choosefromablum = findViewById(R.id.choose_from_ablum);
picture = findViewById(R.id.picture);
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 用來存放攝像頭拍下的圖片,把圖片命名為output_image.jpg
File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//第二種方法(推薦使用第二種方法)
// File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
// if (!file.getParentFile().exists())file.getParentFile().mkdirs();
// Uri imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.sunshunli.cameraalbumtest.fileprovider", file);//通過FileProvider建立一個content型別的Uri
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //新增這一句表示對目標應用臨時授權該Uri所代表的檔案
// intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//設定Action為拍照
// intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//將拍取的照片儲存到指定URI
// startActivityForResult(intent,1006);
if (Build.VERSION.SDK_INT >= 24) {
imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.sunshunli.cameraalbumtest.fileprovider", outputImage);
} else {
//將File物件轉換成URI物件,這個URI物件標識著output_image.jpg這張圖片的本地真是路徑
imageUri = Uri.fromFile(outputImage);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
}
});
choosefromablum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
openAlbum();
}
}
});
}
private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openAlbum();
} else {
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
picture.setImageBitmap(bitmap);
Log.d(TAG, "requestCode:" + requestCode);//1
Log.d(TAG, "data: " + data);//intent{}
Log.d(TAG, "resultCode: " + resultCode);//-1
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
case CHOOSE_PHOTO:
if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT >= 19) {
handleImageOnKitKat(data);
} else {
handleImageBeforeKitKat(data);
}
}
break;
default:
break;
}
}
private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri.getAuthority())){
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID+"="+id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if ("com.android.providers.media.downloads".equals(uri.getAuthority())){
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads.public_downloads"),
Long.valueOf(docId));
imagePath = getImagePath(contentUri,null);
}else if ("content".equalsIgnoreCase(uri.getScheme())){
imagePath = uri.getPath();
}else if ("file".equalsIgnoreCase(uri.getScheme())){
imagePath=uri.getPath();
}
displayImage(imagePath);
}
}
private void handleImageBeforeKitKat(Intent data) {
Uri uri = data.getData();
String imagePath = getImagePath(uri, null);
displayImage(imagePath);
}
private String getImagePath(Uri uri,String selection){
String path = null;
Cursor cursor = getContentResolver().query(uri,null,selection,null,null);
if (cursor.moveToFirst()){
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
return path;
}
private void displayImage(String imagePath){
if (imagePath !=null){
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
picture.setImageBitmap(bitmap);
}else {
Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
}
}
}
大家可以一起交流學習,謝謝!
相關文章
- 在Android中呼叫攝像頭拍照並顯示出來Android
- Android呼叫攝像頭拍照並顯示照片Android
- Android呼叫攝像頭拍照Android
- Android 呼叫攝像頭拍照Android
- Android將view儲存為圖片並放在相簿中AndroidView
- Android上傳圖片之呼叫系統拍照和從相簿選擇圖片Android
- 安卓呼叫攝像頭拍照安卓
- android圓形頭像的選擇和剪下並儲存出圓形圖片Android
- Android 拍照、選擇圖片並裁剪Android
- 照片系列之android呼叫攝像頭拍照Android
- Android開發之呼叫攝像頭拍照Android
- matlab呼叫攝像頭並儲存成幀的形式Matlab
- Android 呼叫攝像頭功能【拍照與視訊】Android
- js將選擇的圖片顯示在img中JS
- 鴻蒙跳轉到相簿或者檔案選擇圖片並顯示到Image控制元件中鴻蒙控制元件
- Android 圓形頭像 相簿和拍照裁剪選取Android
- web呼叫攝像頭拍照並上傳到伺服器Web伺服器
- C#實現攝像頭呼叫、拍照C#
- html5中呼叫攝像頭拍照並上傳(附繞過https的想法)HTMLHTTP
- Android提供的攝像頭拍照Android
- Android呼叫攝像頭Android
- Android拍照,相簿選擇圖片以及Android6.0許可權管理Android
- html5呼叫攝像頭實現拍照HTML
- Qt for Android (三) 開啟Android相簿並選一個圖片進行顯示QTAndroid
- 純JavaScript實現的呼叫裝置攝像頭並拍照的功能JavaScript
- Android如何呼叫攝像頭Android
- iOS開發-新建相簿並儲存圖片到該相簿iOS
- 瀏覽器呼叫攝像頭進行拍照程式瀏覽器
- python版opencv:如何用筆記本攝像頭拍照儲存PythonOpenCV筆記
- Android儲存圖片到系統相簿Android
- C# 使用AForge呼叫筆記本攝像頭拍照C#筆記
- Android本地圖片上傳(拍照+相簿)Android地圖
- WPF呼叫攝像頭,對圖片相似度識別 1.0
- Android學習之呼叫系統相機拍照、截圖並儲存Android
- 使用Hibernate和Struts向資料庫中儲存、讀取並顯示圖片資料庫
- iOS 呼叫系統相簿拍照時顯示英文問題iOS
- android studio呼叫攝像頭拍照及具體步驟演示程式碼Android
- Win10系統開始選單中怎麼新增攝像頭圖示Win10