使用相机拍照并保存到图库 [英] Using camera to take photo and save to gallery

查看:281
本文介绍了使用相机拍照并保存到图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些文档和堆栈,但是我不确定如何实现此目标...

I have went through several documentation and stacks, however I'm not quite sure how do I implement this...

帮助或示例代码确实可以帮助我了解更多信息.

And help or sample codes would really help me understand more.

这是运行相机的一组代码,它运行得很好,我的下一个问题是,如何让它自动保存到手机库中?

Here are the sets of codes that runs the camera and it's working perfectly fine, my next question is, how do I let it automatically saved into phone gallery?

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        tvCameraTiles = (TextView) findViewById(R.id.tvCameraTiles);

        tvCameraTiles.setOnClickListener(new  View.OnClickListener()
        {
             @Override
             public void onClick(View v)
             {
                 dispatchTakePictureIntent();
             }
         });
    }

    private void dispatchTakePictureIntent()
    {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getPackageManager()) != null)
        {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
        {
            //Get Camera Image File Data
//            Bundle extras = data.getExtras();
//            Bitmap imageBitmap = (Bitmap) extras.get("data");
//            mImageView.setImageBitmap(imageBitmap);
        }
    }

推荐答案

您已阅读此 https://developer.android.com/training/camera/photobasics.html 吗?

特别是部分:

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

您似乎没有将照片保存在外部存储设备中,因此应该可以使用.

You don't seem to save the photo in the external storage, so it should work.

我尝试按照文档的方法使用galleryAddPic制作一个非常基本的应用程序.

I tried to make a really basic application following the documentation with the method galleryAddPic.

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

    Button photoButton = (Button) findViewById(R.id.photobutton);
    photoButton.setOnClickListener(new  View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            dispatchTakePictureIntent();
        }
    });
}

static final int REQUEST_IMAGE_CAPTURE = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        //Bundle extras = data.getExtras();
        //Bitmap imageBitmap = (Bitmap) extras.get("data");
        //mImageView.setImageBitmap(imageBitmap);
        galleryAddPic();
    }
}

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    File storageDir = Environment.getExternalStorageDirectory();
    File image = File.createTempFile(
            "example",  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

这对我有用!我使用相机应用程序拍照,然后在图库应用程序中显示. 我修改了有关FileProvider部分的代码,现在它借助方法Uri.fromFile(photoFile)传递了Uri.我也将照片保存在其他位置,请检查createImageFile方法中的代码.

This is working for me! I take a picture using the camera app, and then it's showed in the gallery app. I modified the code regarding the FileProvider part, now it passes the Uri with the help of the method Uri.fromFile(photoFile). I save the photo in a different position too, check the code in the method createImageFile for this one.

在Android Studio模拟器运行正常的情况下,请让我了解您.

In the Android Studio emulator is working fine, let me know about you.

这篇关于使用相机拍照并保存到图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆