我浏览了所有内容,似乎找不到解决我问题的答案。
我的应用程序中有一个图像文件,还有一个按钮...我正在遵循android开发人员https://developer.android.com/training/camera/photobasics#java上的教程,但是在保存文件时遇到了问题。有一次,我能够保存到缓存,但是我宁愿保存到图片中的文件夹。
这是我的代码...
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent dispatchTakePictureIntent = new Intent(MediaStore.actION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (dispatchTakePictureIntent.resolveactivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
photoFile = createImageFile();
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.geturiForFile(this,"com.example.ConfinedSpaceManagement",photoFile);
dispatchTakePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startactivityForResult(dispatchTakePictureIntent,REQUEST_TAKE_PHOTO);
}
}
}
@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);
}
}
String currentPhotoPath;
private File createImageFile(){
// Create an image file name
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!storageDir.exists()) {
if (!storageDir.mkdirs()) {
Log.d("Confined_Space","failed to create Directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(storageDir.getPath() + File.separator + "ConSp" + timeStamp + ".jpg");
}
//String imageFileName = "JPEG_" + timeStamp + "_";
////////////////////////////////////////////////////////////////
static final int REQUEST_TAKE_PHOTO = 1;
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.confinedspacemanagement">
<uses-feature android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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">
<meta-data
android:name="com.google.android.actions"
android:resource="@xml/file_paths" />
<activity android:name=".Signature" />
<activity android:name=".AuditWork" />
<activity android:name=".DisplayIndividual" />
<activity android:name=".Secondactivity" />
<activity android:name=".Definition" />
<activity
android:name=".Mainactivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.confinedspacemanagement.fileprovider"
android:exported="false"
android:granturiPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
</manifest>
这是我的xml.file_path
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>
该活动是一个sqlite表单页面。我正在尝试拍照,将其保存到指定的文件中,然后将文件路径保存到数据库中。
有任何帮助吗???我应该使用camera2吗?