如何在片段中从相机捕获图像,
Posted
技术标签:
【中文标题】如何在片段中从相机捕获图像,【英文标题】:how to capture image from camera, in fragment, 【发布时间】:2014-01-05 09:38:29 【问题描述】:我是 android 新手,我进行了大量培训,但图像无法从相机加载。以下是我从相机或图库中捕获图像的代码:
public void showDiloag()
Dialog dialog = new Dialog(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] "Gallery", "Camera" ,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog,
int which)
switch (which)
case 0:
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent
.createChooser(
intent,
"Choose a Picture");
getAcitivity.startActivityForResult(
chooser,
ACTION_REQUEST_GALLERY);
break;
case 1:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
cameraIntent,
ACTION_REQUEST_CAMERA);
break;
default:
break;
);
builder.show();
dialog.dismiss();
为了展示那张照片:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
System.out.println("OnActivityResult");
if (resultCode == getActivity().RESULT_OK)
if (requestCode == Utils.ACTION_REQUEST_GALLERY)
// System.out.println("select file from gallery ");
Uri selectedImageUri = data.getData();
String tempPath = JuiceAppUtility.getPath(
selectedImageUri, getActivity());
Bitmap bm = JuiceAppUtility
.decodeFileFromPath(tempPath);
imgJuice.setImageBitmap(bm);
else if (requestCode == Utils.ACTION_REQUEST_CAMERA)
Bitmap photo = (Bitmap) data.getExtras()
.get("data");
imgJuice.setImageBitmap(photo);
另外,图像是从相机拍摄的并从图库中选择,但不会加载到ImageView
。有人可以帮帮我吗?
【问题讨论】:
你能给我 JuiceAppUtility.getPath() 函数吗? 【参考方案1】:是的,我发现了你的问题
只需删除下面的行和
getAcitivity.startActivityForResult(
chooser,
ACTION_REQUEST_GALLERY);
并在下面写下代码
startActivityForResult(
chooser,
ACTION_REQUEST_GALLERY);
只需删除 getActivity
【讨论】:
@user2107365 将此标记为已接受的答案,以便其他人知道此问题已解决【参考方案2】:片段面
btnimage.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showPictureDialog();
);
private void showPictureDialog()android.app.AlertDialog.Builder pictureDialog = new android.app.AlertDialog.Builder(getActivity());
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems =
"Select photo from gallery",
"Capture photo from camera";
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
switch (which)
case 0:
// choosePhotoFromGallary();
choosePhotoFromGallary();
break;
case 1:
// takePhotoFromCamera();
takePhotoFromCamera();
break;
);
pictureDialog.show();
private void takePhotoFromCamera()
Uri resultUri;
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, resultUri);
if (intent.resolveActivity(getActivity().getPackageManager()) != null)
startActivityForResult(intent, 22);
public void choosePhotoFromGallary()
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 11);
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK)
if (requestCode == 11)
if (data != null)
Uri contentURI = data.getData();
try
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), contentURI);
// String path = saveImage(bitmap);
// Toast.makeText(getActivity(), "Image Saved!", Toast.LENGTH_SHORT).show();
iv_froncnic.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
Toast.makeText(getActivity(), "Failed!", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "Data not found", Toast.LENGTH_SHORT).show();
在父活动中
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
if (resultCode==this.RESULT_OK)
super.onActivityResult(requestCode, resultCode, data);
setResult(RESULT_OK, data);
【讨论】:
以上是关于如何在片段中从相机捕获图像,的主要内容,如果未能解决你的问题,请参考以下文章
如何在不将图像保存在本地的情况下将捕获的图像(Surface View)传递给另一个片段?
如何在 Android 7.0 中从相机或图库中选择要裁剪的图像?