Taking A Picture With A Camera Intent And Saving It To A File
Solution 1:
Change your onActivityResult
to
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
onCaptureImageResult(data);
}
}
for setting in a thumbnail pass the data
in your ImageView
variable like this:
privatevoidonCaptureImageResult(Intent data) {
Bitmapthumbnail= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStreambytes=newByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
byteArray = bytes.toByteArray();
encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
Filedestination=newFile(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = newFileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(thumbnail);
}
Solution 2:
NullPointerException in two different places, at photoFile.createNewFile();
use this
FilephotoFile=newFile(Environment.getExternalStorageDirectory() + File.separator + "filename");
instead of this
FilephotoFile=null;
NullPointerException in two different places, at image.setImageBitmap(imageBitmap);
just Intilize your ImageView
Solution 3:
File photoFile = null
— since you are assigning null
to photoFile
, you will get a NullPointerException
whenever you try calling methods on photoFile
. You need to set photoFile
to some File
that is covered by your FileProvider
configuration, since you are using FileProvider
. Also note that you can get rid of the createNewFile()
call, as that is not necessary.
And, since you are using EXTRA_OUTPUT
, extras.get("data")
will return null
. Your image is supposed to be in the location indicated by photoFile
.
Here is a sample app demonstrating the use of ACTION_IMAGE_CAPTURE
with FileProvider
.
Post a Comment for "Taking A Picture With A Camera Intent And Saving It To A File"