拍照功能
点击拍照后可以进行拍照,并且将得到的照片展现在屏幕上
添加相机权限
<uses-feature android:name="android.hardware.camera" android:required="true" />
JAVA代码
package Picture; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Bitmap; import android.media.Image; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import com.example.myapp_b.R; public class MainActivity extends AppCompatActivity { ImageView imageView; static final int REQUEST_IMAGE_CAPTURE=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); imageView=findViewById(R.id.iv); } public void takePic(View view) { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if(intent.resolveActivity(getPackageManager())!=null){ startActivityForResult(intent,REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); Bitmap imageBitmap = (Bitmap) extras.get("data"); imageView.setImageBitmap(imageBitmap); } } }
XML代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="Picture.MainActivity" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照" android:onClick="takePic" ></Button> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/iv"></ImageView> </LinearLayout>
保存到本地
package Picture; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.FileProvider; import android.content.Intent; import android.graphics.Bitmap; import android.media.Image; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import com.example.myapp_b.R; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { ImageView imageView; static final int REQUEST_IMAGE_CAPTURE=1; static final int RESULT_OK=-1; String path=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); imageView=findViewById(R.id.iv); } public void takePic(View view) throws IOException { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//开启系统相机的意图对象 String imagePath= Environment.getExternalStorageDirectory()+"/DCIM";//设置照片保存路径 File file=new File(imagePath, "default_image.jpg"); //File file=createImageFile(); path=file.getAbsolutePath(); //使用文件提供者获取路径URL Uri contentUri= FileProvider.getUriForFile(this,"com.example.android.fileprovider",file); //设置传递参数 让相机拍照后保存照片 intent.putExtra(MediaStore.EXTRA_OUTPUT,contentUri); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { //1.设置扫描意图 Intent mediaScanIntent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); //2.设置路径 File f=new File(path); //3.获取路径 Uri contenturi=Uri.fromFile(f);//file:///storage/emulated/0/DCIM/default_image.jpg mediaScanIntent.setData(contenturi); System.out.println(contenturi.toString()); //4.发送广播 this.sendBroadcast(mediaScanIntent); // Bundle extras = data.getExtras(); // Bitmap imageBitmap = (Bitmap) extras.get("data"); // imageView.setImageBitmap(imageBitmap); } } private File createImageFile() throws IOException { // 创建独一无二的文件名 String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents path = image.getAbsolutePath(); return image; } }
配置文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.myapp_b"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <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/Theme.Myapp_b"> <activity android:name="Picture.MainActivity" android:exported="true" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:exported="true"> </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.android.fileprovider" android:exported="false" android:grantUriPermissions="true" tools:ignore="WrongManifestParent"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_path"></meta-data> </provider> </application> </manifest>
file_path.xml
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="DCIM" /> </paths>
不重复文件名
上述方法得到的文件名是重复的,参考官网文档可以将其的名字加上时间
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File file=new File(imagePath, "JPEG_" + timeStamp + ".jpg");