拍照并将其保存在Android的内部存储中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了拍照并将其保存在Android的内部存储中相关的知识,希望对你有一定的参考价值。

我正在尝试使用按钮拍照,并将其保存在内部存储器中并在内部存储器中显示,但是在文件中发现错误...如果我尝试使用局部变量Uri应用程序时崩溃@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

    iv1 = (ImageView)findViewById(R.id.iv);
    b1 = findViewById(R.id.b1);


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

    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }
}

private void takePicture(View v)
{
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     **file** = Uri.fromFile(getOutputMediaFile());

    i.putExtra(MediaStore.EXTRA_OUTPUT, **file**);


    startActivityForResult(i, 100);
}

private static File getOutputMediaFile()
{
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "FotoAula");

    if (!mediaStorageDir.exists()){
        if (!mediaStorageDir.mkdirs()){
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    return new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {

            iv.setImageURI(**file**);
        }
    }
}

}

答案

您可以尝试-->

private static final int CAMERA_IMAGE = 1010;
private Uri imageUri;

private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(Environment.getExternalStorageDirectory(), "my_image.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        imageUri = Uri.fromFile(f);
        startActivityForResult(intent, CAMERA_IMAGE);
    }

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == CAMERA_IMAGE && resultCode == Activity.RESULT_OK) {
                if(imageUri != null){
                    Uri selectedImage = imageUri;
                    getContentResolver().notifyChange(selectedImage, null);
                    Bitmap bitmap = getBitmapFromUri(imageUri.getPath());
                    if(bitmap != null){
                        iv.setImageBitmap(bitmap);                
                    }
                }
            } 
        }

以上是关于拍照并将其保存在Android的内部存储中的主要内容,如果未能解决你的问题,请参考以下文章

Java / Android - 读取、操作 XML 文件并将其保存到内部存储

Android:我需要拍照并将其存储在 sqlite 数据库中,然后将其填充到回收站视图中

Android 实现拍照功能,并将图片保存到本地存储

Android:如何用相机拍照并将位图转换为字节数组并保存到sqlite db?

相机预览没有重新启动?

拍照并保存到 SD 而不损失质量