android如何将捕获的图像保存到手机库中

时间:2011-05-17 07:39:26

标签: android image camera capture

我有两项活动。在一项活动中,我有一个ImageView和一个相机按钮。 当我按下相机按钮时,它会转到其他活动,其中两个按钮在那里捕获,另一个是选择按钮。按下捕获时,它会捕获图像。但问题是如何将此捕获图像保存到图库。按下“选择”按钮后,捕获的图像应显示在第一个活动的ImageView上。 我怎么能这样做。

3 个答案:

答案 0 :(得分:4)

答案 1 :(得分:2)

这就是我这样做的方式。图像在SDCard上以分钟+秒+ .jpg保存:

final static private int NEW_PICTURE = 1;
private String mCameraFileName;


ImageButton Edit = (ImageButton) findViewById(R.id.internetbrowser4);

    Edit.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            // Picture from camera
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

            // This is not the right way to do this, but for some reason, having
            // it store it in
            // MediaStore.Images.Media.EXTERNAL_CONTENT_URI isn't working right.

            Date date = new Date();
            DateFormat df = new SimpleDateFormat("-mm-ss");

            String newPicFile = "Bild"+ df.format(date) + ".jpg";
            String outPath = "/sdcard/" + newPicFile;
            File outFile = new File(outPath);

            mCameraFileName = outFile.toString();
            Uri outuri = Uri.fromFile(outFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);


                startActivityForResult(intent, NEW_PICTURE);

        }
    });

  public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == NEW_PICTURE) 
        {
            // return from file upload
            if (resultCode == Activity.RESULT_OK) 
            {
                Uri uri = null;
                if (data != null) 
                {
                    uri = data.getData();
                }
                if (uri == null && mCameraFileName != null) 
                {
                    uri = Uri.fromFile(new File(mCameraFileName));
                }
                File file = new File(mCameraFileName);
                if (!file.exists()) {
                    file.mkdir();
                }
            }
        }} 
}

答案 2 :(得分:1)

试试这个..

String path = Environment.getExternalStorageDirectory() + "/CameraImages/example.jpg";
                            File file = new File(path);
                            Uri outputFileUri = Uri.fromFile( file );
                            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
                            intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

                            startActivityForResult( intent, CAPTURE_IMAGE );

您的图片将保存在此位置“sdcard / CameraImages / example.jpg”