如何使用相机拍摄新图像并将其存储在外部存储器中?
How can I capture a new image using the camera and after that store it in the external storage?
我正在尝试使用相机拍摄新图像,然后将其存储在外部存储器中。
我遵循了这个教程Save the full-size photo and this Add the photo to a gallery但是在运行应用程序之后,相机启动并成功捕获了图像,但是当我去画廊时,找不到通过以下方式捕获的图像相机!
//I changed this from com.example.android.fileprovider to com.test.app.fileprovider
android:authorities="com.test.app.fileprovider"
//I changed this line Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); to below code
Uri photoURI = FileProvider.getUriForFile(this, "com.test.app.fileprovider", photoFile);
//I added this permission
<uses-permission android:name="android.permission.CAMERA" />
//I enabled storage permission read and write
//I tested the codes on API 24
//minSdkVersion 21
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.app">
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.test.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
</paths>
MainActivity
public class MainActivity extends AppCompatActivity {
String currentPhotoPath;
int REQUEST_IMAGE_CAPTURE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dispatchTakePictureIntent();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.test.app.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
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
currentPhotoPath = image.getAbsolutePath();
return image;
}
}
附加问题:本教程与本教程有何不同Save the full-size photo and this tutorial Add the photo to a gallery?
如果我达到50分,最佳答案将从这个账户中抽取+50 bounty
,否则,我将在2天后从我的其他账户中获得赏金。
您的文件不会被 MediaStore 扫描,因此不会在图库应用程序中显示,因为图库应用程序主要从 MediaStore 获取信息。
getExternalFilesDir()
是您的应用程序的私有位置,MediaStore 尊重这一点。
How can I capture a new image using the camera and after that store it in the external storage?
错误的问题描述。
相机应用拍完照片后,相机应用会将照片保存到文件提供商指定的文件中。那就没什么可做的了。
因此,在启动相机之前,甚至在使用 FileProvider 之前,您应该已经为您的文件确定了一个合适的位置,并为该文件构建了一个很好的 uri。
您至少有两个选择。
使用 MediaStore 获取 public DCIM 或图片目录中文件的 uri。
将 getExternalStoragePublicDirectory()
与 FileProvider 一起使用以获取同一 public 目录中文件的 uri。
我正在尝试使用相机拍摄新图像,然后将其存储在外部存储器中。
我遵循了这个教程Save the full-size photo and this Add the photo to a gallery但是在运行应用程序之后,相机启动并成功捕获了图像,但是当我去画廊时,找不到通过以下方式捕获的图像相机!
//I changed this from com.example.android.fileprovider to com.test.app.fileprovider
android:authorities="com.test.app.fileprovider"
//I changed this line Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); to below code
Uri photoURI = FileProvider.getUriForFile(this, "com.test.app.fileprovider", photoFile);
//I added this permission
<uses-permission android:name="android.permission.CAMERA" />
//I enabled storage permission read and write
//I tested the codes on API 24
//minSdkVersion 21
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.app">
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.test.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
</paths>
MainActivity
public class MainActivity extends AppCompatActivity {
String currentPhotoPath;
int REQUEST_IMAGE_CAPTURE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dispatchTakePictureIntent();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.test.app.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
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
currentPhotoPath = image.getAbsolutePath();
return image;
}
}
附加问题:本教程与本教程有何不同Save the full-size photo and this tutorial Add the photo to a gallery?
如果我达到50分,最佳答案将从这个账户中抽取+50 bounty
,否则,我将在2天后从我的其他账户中获得赏金。
您的文件不会被 MediaStore 扫描,因此不会在图库应用程序中显示,因为图库应用程序主要从 MediaStore 获取信息。
getExternalFilesDir()
是您的应用程序的私有位置,MediaStore 尊重这一点。
How can I capture a new image using the camera and after that store it in the external storage?
错误的问题描述。
相机应用拍完照片后,相机应用会将照片保存到文件提供商指定的文件中。那就没什么可做的了。
因此,在启动相机之前,甚至在使用 FileProvider 之前,您应该已经为您的文件确定了一个合适的位置,并为该文件构建了一个很好的 uri。
您至少有两个选择。
使用 MediaStore 获取 public DCIM 或图片目录中文件的 uri。
将 getExternalStoragePublicDirectory()
与 FileProvider 一起使用以获取同一 public 目录中文件的 uri。