android


Can't get camera code from Android docs to work


I am using code from http://developer.android.com/training/camera/photobasics.html
Code:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = FileUtilities.createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(getActivity(),"Error!",Toast.LENGTH_SHORT).show();
}
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras(); //error
//code after this doesn't get executed
}
}
I'm trying to get access to the thumbnail and the picture being stored on the device. But for some reason when I try the code, I get a null pointer exception on the data.getExtras(); part.
What am I missing here?
That's one of notorious Android development experience.
Android Intent does not guarantee to give captured image in data.getExtras(), especially user utilize 3rd party camera/imaging app. You can find many trials and suggestions in here and anywhere googled with "android camera intent null".
Some common of them are as below.
data.getExtras().get("data");
data.getExtras() with different key (i.e "photo")
data.getData()
Uri.fromFile(f) for EXTRA_OUTPUT predefined path.
Uri.fromFile(f) with some random filename (datetime format or IMG-xxx) without maintaining EXTRA_OUTPUT definition.
I recommend you to find it using breakpoint which route of the variable that the intent given. It would be good to check all of them in if-else if-else approach.
In addition, check out crash report carefully after releasing the app. You may get the error out of the above trials.
To get the ThumbNail, you don't need to create a file etc. please try this code below.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
and to get the results.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imgView.setImageBitmap(imageBitmap);
}
}
imgView is the ImageView you want to set the ThumbNail to.
In case if you want to create a file and then try this, [ which is not needed for a Thumbnail], you may want to try adding the following permission to manifest as you are trying to read and write to storage.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
All the best.

Related Links

android SQLiteConstraintException: error code 19: constraint failed
Android Eclipse library project and the library project's type resolution for project specific config files
How to override SearchRecentSuggestionsProvider
Audio recording issue android
open different activities from map overlays
Unable to execute dex: wrapper was not properly loaded first
Sleeping service with contentobserver
Pressing Back Button did not go back to previous activity Android
Problems with org.apache.harmony.xml.ExpatParser$ParseException
How to reduce VideoView buffer with device to device RTSP streaming
android - Accelerations work only in debug
Can I alter the order that points in an Maps Overlay are drawn on the Map
Android - How can I programmatically add a VPN network
How to handle empty space in url when downloading image from web?
Multi-table database in Android app
add image to surface view in android

Categories

HOME
cygwin
scikit-learn
turing-machines
deep-learning
gpu
jax-rs
jwplayer
requirements
nuget
replace
babeljs
bookmarks
echo
google-cloud-datalab
augeas
project-intu
spring-social
selenium-ide
heat
mongodb-csharp
libusb
package-managers
esoteric-languages
job-scheduling
android-7.0-nougat
vuejs
foreign-keys
symfony-console
kendo-mobile
jquery-ias
epsilon
comparator
roundup
draft-js-plugins
google-cloud-print
surveymonkey
pic32
custom-post-type
fable-f#
asymptotic-complexity
tizen-tv
installscript
html-encode
xajax
hivemq
zoomify
modernizr
odroid
urlsession
mcustomscrollbar
vmware-fusion
show-hide
jide
jboss-4.2.x
gitlist
wcftestclient
dtsearch
trojan
yii2-user
git-flow
d3-force-directed
gameplay-kit
segment-io
burrows-wheeler-transform
mta
firebase-security
lua-telegram-bot
tracker-enabled-dbcontext
uiactionsheet
yajsw
lz77
pushbots
user-management
spring-retry
extjs-grid
visual-sourcesafe-2005
mod-proxy
wso2greg
xmltype
boost-program-options
crowd
appserver
authlogic
high-resolution
gwt-openlayers
sphinxql
epiceditor
extreme-programming
patricia-trie
os.system
fusefabric
josql
role
workflow-services
fsevents
catransform3d
timthumb
eclipse-marketplace
glui
feasibility
contentpresenter

Resources

Encrypt Message