面对相机应用程序的问题

时间:2014-07-08 08:44:11

标签: android camera android-sdcard

我正在开发相机应用程序,我必须单击该图像  并将其保存在新创建的SD卡中的图像文件夹中

这是我的MainActivity.java文件。

 public class MainActivity extends Activity {


        Button cambutton;
        ImageView photoImage=null;

        private static final String TAG = "CallCamera";
        private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;

        Uri fileUri=null;
        //ImageView photoImage1 = null;

        private File getOutputPhotoFile() {

              File directory = new File(Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_PICTURES), getPackageName());

              if (!directory.exists()) {
                if (!directory.mkdirs()) {
                  Log.e(TAG, "Failed to create storage directory.");
                  return null;
                }
              }

              String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.US).format(new Date());

              return new File(directory.getPath() + File.separator + "IMG_"  
                                + timeStamp + ".jpg");
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button camButton = (Button)findViewById(R.id.btncam);
            ImageView photoImage = (ImageView)findViewById(R.id.iv);

            camButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Intent i = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);

                    fileUri = Uri.fromFile(getOutputPhotoFile());
                    i.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);

                    startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ);

                }
            });

        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
                if (resultCode == RESULT_OK) {
                  Uri photoUri = null;
                  if (data == null) {
                    // A known bug here! The image should have saved in fileUri
                    Toast.makeText(this, "Image saved successfully", 
                                   Toast.LENGTH_LONG).show();
                    photoUri = fileUri;
                  } else {
                    photoUri = data.getData();
                    Toast.makeText(this, "Image saved successfully in: " + data.getData(), 
                                   Toast.LENGTH_LONG).show();
                  }
                  showPhoto(photoUri.getPath());


                } else if (resultCode == RESULT_CANCELED) {
                  Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
                } else {
                  Toast.makeText(this, "Callout for image capture failed!", 
                                 Toast.LENGTH_LONG).show();
                }
              }
        }

        private void showPhoto(String photoUri) {
              File imageFile = new File (photoUri);
              if (imageFile.exists()){
                 Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
                 BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
                 photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
                 photoImage.setImageDrawable(drawable);
              }       
        }

    }

当我运行它时,它运行得很好但是当我尝试保存图像时,应用程序崩溃了。

我的logcat如下所示。

 07-08 03:51:02.976: W/dalvikvm(2104): threadid=1: thread exiting with uncaught exception (group=0xb1d4bb20)
07-08 03:51:02.986: E/AndroidRuntime(2104): FATAL EXCEPTION: main
07-08 03:51:02.986: E/AndroidRuntime(2104): Process: com.dev.camera, PID: 2104
07-08 03:51:02.986: E/AndroidRuntime(2104): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.dev.camera/com.dev.camera.MainActivity}: java.lang.NullPointerException
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.os.Looper.loop(Looper.java:136)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at java.lang.reflect.Method.invoke(Method.java:515)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at dalvik.system.NativeStart.main(Native Method)
07-08 03:51:02.986: E/AndroidRuntime(2104): Caused by: java.lang.NullPointerException
07-08 03:51:02.986: E/AndroidRuntime(2104):     at com.dev.camera.MainActivity.showPhoto(MainActivity.java:126)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at com.dev.camera.MainActivity.onActivityResult(MainActivity.java:95)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
07-08 03:51:02.986: E/AndroidRuntime(2104):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
07-08 03:51:02.986: E/AndroidRuntime(2104):     ... 11 more
07-08 03:51:08.536: I/Process(2104): Sending signal. PID: 2104 SIG: 9

1 个答案:

答案 0 :(得分:0)

1)点击事件:     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                     startActivityForResult(intent,1);

2)inActivityResult:

if ((requestCode == 1)) {

                // -----------path getting from intent------------
                String photoPath = null;
                Cursor cursor = getContentResolver().query(
                        Media.EXTERNAL_CONTENT_URI,
                        new String[] { Media.DATA,         Media.DATE_ADDED,MediaStore.Images.ImageColumns.ORIENTATION },
                        Media.DATE_ADDED, null, "date_added ASC");
                if (cursor != null && cursor.moveToFirst()) {
                    do {
                        Uri uri = Uri.parse(cursor.getString(cursor    .getColumnIndex(Media.DATA)));
                        photoPath = uri.toString();

                    } while (cursor.moveToNext());
                    cursor.close();
                }
 }

3)在你的清单中: