所以我试着做4件事: 1)拍照 2)将其加载到ImageView中 3)制作一个新目录 4)将图像保存在该目录中。
问题在于:
File path = Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES);
File mediaStorageDir = new File(path, "MyCameraApp");
由于某种原因,它没有创建一个名为MyCameraApp的新文件。
和
我一直得到threadid = 1:线程退出时有未捕获的异常(group = 0x41631ba8)。
这是我的代码:
public class Main extends Activity {
private Uri fileUri;
ImageView iv;
private Camera mCamera;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.imageView);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
Button btn = (Button) findViewById(R.id.takePhoto);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
}
}
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File mediaStorageDir = new File(path, "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
File mediaFile = null;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + /*File.separator +*/ "IMG_1" +".tif");
Log.d("MyCameraApp", "DONE");
}
else {
Log.d("MyCameraApp", "NOT DONE!");
return null;
}
return mediaFile;
}
protected void onPause(){
super.onPause();
releaseCamera(); // release the camera immediately on pause event
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}
StackTrace:
03-10 17:14:39.708: I/Process(9786): Sending signal. PID: 9786 SIG: 9
03-10 18:04:04.248: D/MyCameraApp(13459): failed to create directory
03-10 18:04:04.248: D/AndroidRuntime(13459): Shutting down VM
03-10 18:04:04.248: W/dalvikvm(13459): threadid=1: thread exiting with uncaught exception(group=0x41631ba8)
03-10 18:04:04.258: E/AndroidRuntime(13459): FATAL EXCEPTION: main
03-10 18:04:04.258: E/AndroidRuntime(13459): Process: com.example.irisrec, PID: 13459
03-10 18:04:04.258: E/AndroidRuntime(13459): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.irisrec/com.example.irisrec.Main}: java.lang.NullPointerException: file
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-10 18:04:04.258: E/AndroidRuntime(13459): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.os.Handler.dispatchMessage(Handler.java:102)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.os.Looper.loop(Looper.java:136)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-10 18:04:04.258: E/AndroidRuntime(13459): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 18:04:04.258: E/AndroidRuntime(13459): at java.lang.reflect.Method.invoke(Method.java:515)
03-10 18:04:04.258: E/AndroidRuntime(13459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-10 18:04:04.258: E/AndroidRuntime(13459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-10 18:04:04.258: E/AndroidRuntime(13459): at dalvik.system.NativeStart.main(Native Method)
03-10 18:04:04.258: E/AndroidRuntime(13459): Caused by: java.lang.NullPointerException: file
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.net.Uri.fromFile(Uri.java:444)
03-10 18:04:04.258: E/AndroidRuntime(13459): at com.example.irisrec.Main.getOutputMediaFileUri(Main.java:80)
03-10 18:04:04.258: E/AndroidRuntime(13459): at com.example.irisrec.Main.onCreate(Main.java:38)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.Activity.performCreate(Activity.java:5231)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-10 18:04:04.258: E/AndroidRuntime(13459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
感谢您的帮助!!