gpt4 book ai didi

java - 以编程方式从相机文件夹获取图像

转载 作者:太空宇宙 更新时间:2023-11-04 15:13:27 24 4
gpt4 key购买 nike

各位。

我在这里面临一个奇怪的问题。我有一个应用程序,您可以通过单击按钮从图库或相机中选择图像。一切都很顺利,除了每当我从相机文件夹中选择任何图片时,应用程序就会崩溃。下面是我的代码

case R.id.camera:

AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);

builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Gallery", "Camera" },
new DialogInterface.OnClickListener() {

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");
startActivityForResult(chooser,
ACTION_REQUEST_GALLERY);

break;

case 1:

Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String mImageCaptureUri = null;
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(cameraIntent,
ACTION_REQUEST_CAMERA);

break;

}
}
});

builder.show();

break;

结果 Activity :

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

switch (requestCode) {
case ACTION_REQUEST_GALLERY:


Uri selectedImageUri;

selectedImageUri = data == null ? null : data.getData();

cap.setImageURI(selectedImageUri);



break;

case ACTION_REQUEST_CAMERA:

Bitmap photo = (Bitmap) data.getExtras().get("data");

cap.setImageBitmap(photo);

// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);

// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));

Toast.makeText(getApplicationContext(),
finalFile.getAbsolutePath(), 4000).show();

break;
}

}
}

最佳答案

没有logcat很难说,但最可能的原因是图像尺寸大,用相机拍摄的照片太大而无法在不缩放的情况下显示,这就是您尝试的某些图像有效而相机图像无效的原因。尝试谷歌:缩放大位图谷歌开发人员,你会找到官方帖子

关于java - 以编程方式从相机文件夹获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103401/

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