我正在尝试创建一个应用程序,我可以拍摄新照片或使用现有照片 . 我让两个函数分开工作但不能一起工作 . 当Uri imageUri = data.getData()时,应用程序崩溃;运行 .
这是代码:
public void btnPhotoClicked(View v) {
//Use to invoke a Camera
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Create a variable with the filepath generated by the android operating system, Like a baws
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
if (pictureDirectory.exists()) {
File test1 = new File(pictureDirectory, "100MEDIA/");
if (test1.exists()) {
pictureDirectory = test1;
} else {
File test2 = new File(pictureDirectory, "100ANDRO/");
if (test2.exists()) {
pictureDirectory = test2;
} else {
File test3 = new File(pictureDirectory, "Camera/");
if (!test3.exists()) {
test3.mkdirs();
}
pictureDirectory = test3;
}
}
} else {
pictureDirectory = new File(pictureDirectory, "Camera/");
pictureDirectory.mkdirs();
}
String pictureName = getPictureName();
File imageFile = new File(pictureDirectory, pictureName);
Uri pictureUri = Uri.fromFile(imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
//Start intent and anticipate result
startActivityForResult(cameraIntent, CAMERA_RESULT);
}
这里是受保护的void onActivityResult的代码(int requestCode,int resultCode,Intent data)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//if we are here everything processed sucessfully
if (requestCode==IMAGE_GALLERY_REQUEST) {
//If we are here we came from the Välj existerande bild
Log.d("MainActivity", data.toString());
Toast.makeText(this, "IMAGE GALLERY_REQUEST", Toast.LENGTH_LONG).show();
//the adress of the image on the device SD-card
Uri imageUri = data.getData();
//Declare a stream to read the image data from the SD-card
InputStream inputStream;
//Getting a Input stream based upon the image uri
try {
//if it execute flawlessly
inputStream = getContentResolver().openInputStream(imageUri);
Bitmap image = BitmapFactory.decodeStream(inputStream);
//show the image to the user
mImageView.setImageBitmap(image);
} catch (FileNotFoundException e) {
//show massage saying if the image is unavailable
Toast.makeText(this, "Could not open image", Toast.LENGTH_LONG).show();
}
} else if (requestCode == CAMERA_RESULT) {
//We are here because we have received a result from the camera
Toast.makeText(this, "CAMERA RESULT", Toast.LENGTH_LONG).show();
Log.d("MainActivity", data.toString());
//the adress of the image on the device SD-card
Uri imageUri = data.getData();
//Declare a stream to read the image data from the SD-card
InputStream inputStream;
//Getting a Input stream based upon the image uri
try {
//if it execute flawlessly
inputStream = getContentResolver().openInputStream(imageUri);
Bitmap image = BitmapFactory.decodeStream(inputStream);
//show the image to the user
mImageView.setImageBitmap(image);
} catch (FileNotFoundException e) {
//show massage saying if the image is unavailable
Toast.makeText(this, "Could not open image", Toast.LENGTH_LONG).show();
}
}
}
非常感谢你 :)
2 回答
试试这个代替图像uri
只有在返回的Intent中传回缩略图时,默认的Android相机应用程序才会返回非空的intent . 如果传递带有要写入的URI的
EXTRA_OUTPUT
,它将返回null
intent,并且图片位于您传入的URI中 .您可以通过查看GitHub上的相机应用程序源代码来验证这一点:
你的应用程序崩溃了 . 因为
getContentResolver().openInputStream(imageUri)
调用null对象的方法 .imageUri
为空 .来自documentation:
所以,你调用
ACTION_IMAGE_CAPTURE
intent,你应该像这样传递额外的:然后在onActivityResult中:
UPDATED