gpt4 book ai didi

java - 由于相机上的错误位图 URI,resolveUri 失败

转载 作者:行者123 更新时间:2023-12-02 03:23:36 24 4
gpt4 key购买 nike

我在尝试从相机和图像选择器创建位图时遇到问题。

我使用了由相机创建 Uri 的代码,因此我向已从图库加载图片的函数添加了一个条件。这是 onActivityResult :

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE || requestCode == RESULT_CAMERA) {
Uri selectedImage = null;
if(requestCode == RESULT_LOAD_IMAGE)
{
selectedImage = data.getData();
}
else if(requestCode == RESULT_CAMERA)
{
selectedImage = imageUri;
}
if(resultCode == RESULT_OK && null != data) {

String[] filePathColumn = {MediaStore.Images.Media.DATA};
imgViewScan.setImageURI(selectedImage);
try {
InputStream stream = getContentResolver().openInputStream(
selectedImage);
bitmapLoaded = BitmapFactory.decodeStream(stream);
} catch (IOException e) {
Log.e("ScanAc", e.toString());
}
}


}
}

这是相机的 onClick :

View.OnClickListener takePicture = new View.OnClickListener() {
public void onClick(View v) {

String fileName = "new-photo-name.jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
Intent i = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(i, RESULT_CAMERA);
}
};

我想准确地说,图库图像选择工作完美,问题仅出在相机上......

最佳答案

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

if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {

ContentValues values = new ContentValues(1);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(takePictureIntent, SELECT_PICTURE_CAMARA);

} else {
Toast.makeText(this, getString(R.string.error_no_camera), Toast.LENGTH_LONG).show();
}

}

try this in your takePicture clickListener

关于java - 由于相机上的错误位图 URI,resolveUri 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com