android打开系统相机并保存到拍照并显示到屏幕上
时间: 2023-12-16 16:03:36 浏览: 52
好的,下面是一份简单的代码示例,可以帮助你实现在 Android 应用程序中打开系统相机并保存拍摄的照片并显示到屏幕上:
```java
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取 ImageView 实例
mImageView = findViewById(R.id.imageView);
// 创建一个 Intent 对象,启动系统相机
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 检查设备是否有相机应用程序
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// 创建一个临时文件来保存拍摄的照片
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// 处理异常
}
// 如果文件创建成功,将其作为输出文件传递给相机应用程序
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 检查请求代码是否匹配,并且结果代码是否为 RESULT_OK
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// 将拍摄的照片显示在 ImageView 上
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.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, /* 前缀 */
".jpg", /* 后缀 */
storageDir /* 目录 */
);
// 保存文件路径以供后面使用
String currentPhotoPath = image.getAbsolutePath();
return image;
}
}
```
在这个示例中,我们创建了一个 `Intent` 对象来启动系统相机应用程序。然后,我们检查设备是否有相机应用程序,并在创建一个临时文件以保存拍摄的照片之后,将其作为输出文件传递给相机应用程序。在拍摄完成后,我们在 `onActivityResult()` 方法中获取拍摄的照片并将其显示在 `ImageView` 上。
注意,这个示例需要你在应用程序的 `AndroidManifest.xml` 文件中添加以下代码,以声明你的应用程序需要访问外部存储器:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
此外,还需要在 `AndroidManifest.xml` 文件中添加以下代码,以定义一个 FileProvider,以便在应用程序间共享文件:
```xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
```
最后,你需要在 `res/xml/` 目录下创建一个名为 `file_paths.xml` 的文件,并添加以下代码:
```xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.android/files/Pictures" />
</paths>
```
这个文件定义了 `FileProvider` 可以访问的路径。在这个示例中,我们允许访问应用程序的外部存储器的 Pictures 目录。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)