【发布时间】:2016-05-04 22:24:34
【问题描述】:
在我的应用程序中,我正在尝试使用从Android Developers tutorial 获得的代码来拍照、保存并添加到 ImageView 中
private static final int REQUEST_IMAGE_CAPTURE = 1;
private String lastImagePath;
//Create temporary image file using getExternalFilesDir()
private File createImageFile() throws IOException{
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timestamp + "_";
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
lastImagePath = image.getAbsolutePath();
return image;
}
//Start picture intent and save to file
private void takePicture(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
if(takePictureIntent.resolveActivity(getActivity().getPackageManager())!= null){
try{
photoFile = createImageFile();
}catch (IOException e){
Log.e(TAG, "Error Creating image file");
e.printStackTrace();
}
if (photoFile !=null){
Log.e(TAG, photoFile.getAbsolutePath());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK){
//Ensure the path has been set
if(lastImagePath !=null){
Log.e(TAG, "LastImagePath: " + lastImagePath);
//DEBUG:see if it actually created the file
File file = new File(lastImagePath);
if(file.exists()){
Log.e(TAG, "File exists");
}
//Decode file
Bitmap image = BitmapFactory.decodeFile(lastImagePath);
//DEBUG: see if image is null
if(image == null){
Log.e(TAG, "Image is null");
}else{
Log.e(TAG, "Image is not null");
}
//convert full sized image to thumbnail and add it to imageView
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, 200, 200);
image1.setImageBitmap(thumbnail);
}else{
Log.e(TAG, "lastImagePath is null");
}
}
}
有时此代码会按预期工作,并获取图像、保存图像、将其加载回程序并将其添加到图像视图中。但其他时候,它根本不起作用,似乎无法拍摄图像。
这是成功捕获图像的 logcat 输出,其中图像出现在 ImageView 中
05-04 17:27:49.218 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172748_-1847663626.jpg
05-04 17:28:03.886 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: LastImagePath: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172748_-1847663626.jpg
05-04 17:28:04.359 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: File exists
05-04 17:28:04.825 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: Image is not null
这是一个失败的图像捕获的 logcat 输出,其中 ImageView 中没有图像
05-04 17:29:00.070 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172900_-1359930406.jpg
05-04 17:29:06.581 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: LastImagePath: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172900_-1359930406.jpg
05-04 17:29:06.583 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: File exists
05-04 17:29:06.583 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: Image is null
这似乎是随机工作的,并且 80% 的时间都失败了。我使用 getExternalFilesDir 因为我希望照片对应用程序是私有的。我还在我的 Manifest 文件中添加了 WRITE_EXTERNAL_STORAGE 权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我也知道我可以使用图像中的缩略图,但我想要完整大小的图像,因为我稍后会将它添加到课程中。
有人对出了什么问题有任何想法吗?
【问题讨论】: