[英](Android) Camera Capture not saving, can't load bitmap of image
I'm trying to write code that launches the camera app, saves the image, and then displays a bitmap of the captured image. 我正在尝试编写代码,以启动相机应用程序,保存图像,然后显示捕获图像的位图。
This is the relevant code, which was adapted from https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media : 这是相关的代码,该代码改编自https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media :
public static final int PICTURE_REQUEST_CODE = 1034;
public static final String FILE_BEGIN = "note";
private int note_id = 4;
public static final String FILE_END = ".jpg";
private static final String APP_TAG = "School_App";
private void takePicture() {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(FILE_BEGIN + note_id + FILE_END));
if (i.resolveActivity(getPackageManager()) != null){
startActivityForResult(i, PICTURE_REQUEST_CODE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICTURE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Uri takenPhotoUri = getPhotoFileUri(FILE_BEGIN + note_id + FILE_END);
Log.d("NotesDetail", takenPhotoUri.toString());
// by this point we have the camera photo on disk
Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
// RESIZE BITMAP, see section below
// Load the taken image into a preview
ImageView ivPreview = (ImageView) findViewById(R.id.iv_notes_picture);
ivPreview.setImageBitmap(takenImage);
} else { // Result was a failure
Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
}
private Uri getPhotoFileUri(String fileName) {
if(isSpaceAvailable()){
File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
Log.d(APP_TAG, "Failed to make directory");
}
File file = new File(mediaStorageDir.getPath() + File.separator + fileName);
Uri uri = FileProvider.getUriForFile(NotesDetail.this, "com.kfode.schoolapp.fileprovider", file);
Log.d("getPhotoFileUri", uri.toString());
return uri;
}
return null;
}
private boolean isSpaceAvailable() {
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED);
}
I also have the following permissions set: 我还设置了以下权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Within the application tag of the manifest... 在清单的application标签内...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.kfode.schoolapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
/>
</provider>
And within the file_paths.xml 并在file_paths.xml中
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="images"
path="Pictures"/>
When I run the app, I get the following log messages and exception: 运行该应用程序时,我收到以下日志消息和异常:
D/getPhotoFileUri: content://com.kfode.schoolapp.fileprovider/images/School_App/note4.jpg
D/NotesDetail: content://com.kfode.schoolapp.fileprovider/images/School_App/note4.jpg
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /images/School_App/note4.jpg: open failed: ENOENT (No such file or directory)
Not sure what is going on here. 不知道这是怎么回事。 Help please?
请帮助?
Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
This will not work. 这是行不通的。 A
Uri
is not a File
. Uri
不是File
。
Refactor getPhotoFileUri()
into two methods: 将
getPhotoFileUri()
重构为两种方法:
getPhotoFile()
that generates your File
File
getPhotoFile()
getPhotoFileUri()
that obtains the FileProvider
Uri
for that File
getPhotoFileUri()
获得该File
的FileProvider
Uri
Then, use an image-loading library (Glide, Picasso, etc.) to populate your ImageView
from the file returned by getPhotoFile()
. 然后,使用图像加载库(Glide,Picasso等)从
getPhotoFile()
返回的文件中填充ImageView
。 This not only will handle all the BitmapFactory
stuff for you, but it will also do that work on a background thread, so you do not freeze your UI the way you are in your current code. 这不仅将为您处理所有
BitmapFactory
内容,而且还将在后台线程上完成该工作,因此您不会像在当前代码中那样冻结UI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.