我的一个片段包含一个简单的相机应用程序。它应该拍照,然后在imageView中永久显示照片。我将新创建的文件传入我的Camera Intent时收到文件未找到错误。我不确定为什么会这样。我的意图仍然会通过,我可以拍照,但它不会保存到文件。我的代码如下。
代码:
package layout;
import com.tble.brgo.R;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.io.File;
import java.io.FileFilter;
import java.util.Date;
import android.net.Uri;
import android.widget.ImageView;
import java.io.IOException;
import java.text.SimpleDateFormat;
import android.content.ContentValues;
import android.provider.MediaStore.Images;
/**
* A simple {@link Fragment} subclass.
* Use the {@link StudentID#newInstance} factory method to
* create an instance of this fragment.
*/
public class StudentID extends Fragment {
String mCurrentPhotoPath;
Uri mPhotoURI;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
ImageView picView;
FloatingActionButton button;
public StudentID() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment StudentID.
*/
// TODO: Rename and change types and number of parameters
public static StudentID newInstance() {
StudentID fragment = new StudentID();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_student_id, container, false);
picView = (ImageView)view.findViewById(R.id.idPic);
picView.setScaleType(ImageView.ScaleType.FIT_XY);
button = (FloatingActionButton)view.findViewById(R.id.picButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePicture();
}
});
picView = (ImageView)view.findViewById(R.id.idPic);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
picView.setImageURI(Uri.parse(sharedPref.getString("StudentID","")));
return view;
}
public void takePicture(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getContext().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) {
try {
mPhotoURI = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
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 = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = new File(storageDir,imageFileName + ".jpg");
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == android.app.Activity.RESULT_OK){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this.getContext());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("StudentID", mPhotoURI.toString());
picView.setImageURI(mPhotoURI);
}
}
}
错误:
12-19 19:09:50.238 5705-5705/com.tble.brgo W/ImageView: Unable to open content: file:///data/user/0/com.tble.brgo/files/JPEG_20161219_190950_.jpg
java.io.FileNotFoundException: /data/user/0/com.tble.brgo/files/JPEG_20161219_190950_.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:487)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at android.content.ContentResolver.openInputStream(ContentResolver.java:676)
at android.widget.ImageView.resolveUri(ImageView.java:832)
at android.widget.ImageView.setImageURI(ImageView.java:449)
at layout.StudentID.takePicture(StudentID.java:107)
at layout.StudentID$1.onClick(StudentID.java:78)
at android.view.View.performClick(View.java:5265)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:473)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at android.content.ContentResolver.openInputStream(ContentResolver.java:676)
at android.widget.ImageView.resolveUri(ImageView.java:832)
at android.widget.ImageView.setImageURI(ImageView.java:449)
at layout.StudentID.takePicture(StudentID.java:107)
at layout.StudentID$1.onClick(StudentID.java:78)
at android.view.View.performClick(View.java:5265)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:2)
您致电startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
。这意味着如果你很幸运,你会在几秒钟或几分钟后拍照。
在下一个声明中,您将无法获得图片,其中包含picView.setImageURI(photoURI);
。
将picView.setImageURI(photoURI);
移至onActivityResult()
,并仅在实际获得图片时调用它(即结果为ACTIVITY_OK
)。