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

发布于 2024-11-08 05:27:06 字数 180 浏览 0 评论 0原文

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

I have two activities. In one activity, I have an ImageView and an camera button.
When I press camera button it goes to other activity where two buttons are there Capture and another is Select button. When I press capture it captures an image. But question is how to save this capture image to gallery. And after pressing the Select button the captured image should display on 1st activity's ImageView.
How can I do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妞丶爷亲个 2024-11-15 05:27:06

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

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();
                }
            }
        }} 
}

This is the way I did it. The image is saved by minutes+seconds+.jpg on the SDCard:

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();
                }
            }
        }} 
}
沦落红尘 2024-11-15 05:27:06

试试这个..

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”

try this..

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 );

your image will be save at this location "sdcard/CameraImages/example.jpg"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文