-2

如何将相机通过我的应用程序拍摄的照片保存到设备的图库中?

问题是应用程序目前在拍照后崩溃,并且只能在指定的目录中保存单个图像。我是否正确地假设应用程序崩溃是因为它不知道保存文件后该做什么?我该如何解决这个问题,return 语句之后的任何内容都是“无法访问的代码”。

编码:

public class Activity_Camera extends Activity implements View.OnClickListener {

        public static final int cameraData = 1;

        ImageButton ib;
        ImageView iv;
        Intent i;
        Bitmap bmp;

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

        private void initialise() {
                iv = (ImageView) findViewById(R.id.ivPicReturn);
                ib = (ImageButton) findViewById(R.id.ibTakePic);

                ib.setOnClickListener(this);
        }

        public void onClick(View v) {
                switch (v.getId()) {
                case R.id.ibTakePic:
                        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        //
                        i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());              
                        //
                        startActivityForResult(i, cameraData);
                        break;
                }
        }
        //
        private Uri getOutputMediaFileUri() {
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "DCIM/TradeIt");
            imagesFolder.mkdirs();  
            File image = new File(imagesFolder, "image.jpg");
            Uri uriSavedImage = Uri.fromFile(image);
            return uriSavedImage;


        }
        //
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == RESULT_OK) {
                        Bundle extras = data.getExtras();
                        bmp = (Bitmap) extras.get("data");
                        iv.setImageBitmap(bmp);


                }

        }

}

日志猫

W/dalvikvm(919): threadid=1: thread exiting with uncaught exception (group=0x41d7d930) 
03-07 00:00:24.704: E/AndroidRuntime(919): FATAL EXCEPTION: main 
03-07 00:00:24.704: E/AndroidRuntime(919): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.hyperlocal.tradeit/com.hyperlocal.tradeit.Activity_Camera}: java.lang.NullPointerException 
03-07 00:00:24.704: E/AndroidRuntime(919): at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
4

1 回答 1

1

您必须增加一个变量并将其保存并将其添加到您的文件名中,以便在您获取 Uri 时制作更多图片,并且要让图片显示在您的图库中,请执行以下操作:

   sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" +    Environment.getExternalStorageDirectory())));

如果你拍照后不发送这样的广播,画廊将不会更新,直到下次手机打开

于 2013-03-07T00:10:12.257 回答