Eduardo Wada
Eduardo Wada

Reputation: 2647

Error when taking picture on emulated android camera

I'm trying to use an emulator in ADT to test an app that takes pictures, I'm able to start the camera but after taking the picture and clicking the "confirm" button logcat throws an error:

08-21 13:46:18.933: E/SoundPool(289): error loading /system/media/audio/ui/Effect_Tick.ogg

After this, the callback event in my application is not called and I'm unable to get the picture, I can however, click "cancel" and callback is called with code RESULT_CANCELED.

Here's my code (part of):

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
    imagesFolder.mkdirs(); // <----
    File image = new File(imagesFolder, "image_001.jpg");
    Uri fileUri = Uri.fromFile(image);

    i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(i, 100);
    ...
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 100){     
        if (resultCode == RESULT_OK){       
        } else if (resultCode == RESULT_CANCELED){      
        } else {        
        }
    }
}

I "could" test my code on a real device but I'd really like to fix my emulator, any idea how to do this?

--EDIT--

This is my AVD's config file:

avd.ini.encoding=ISO-8859-1
hw.dPad=no
hw.lcd.density=320
sdcard.size=200M
hw.cpu.arch=arm
hw.device.hash=298918422
hw.camera.back=emulated
disk.dataPartition.size=200M
skin.dynamic=yes
skin.path=768x1280
hw.keyboard=yes
hw.cpu.model=cortex-a8
hw.ramSize=768
hw.device.manufacturer=Google
hw.sdCard=yes
hw.mainKeys=no
hw.accelerometer=yes
skin.name=768x1280
abi.type=armeabi-v7a
hw.trackBall=no
hw.device.name=Nexus 4
hw.battery=yes
hw.sensors.proximity=yes
image.sysdir.1=system-images\android-18\armeabi-v7a\
hw.sensors.orientation=yes
hw.audioInput=yes
hw.camera.front=emulated
hw.gps=yes
vm.heapSize=64

I have also added markup below in my manifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Upvotes: 1

Views: 2857

Answers (2)

Eduardo Wada
Eduardo Wada

Reputation: 2647

I found that the problem was being caused by the app trying to save the picture file, I removed the lines below and it started working

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs(); // <----
File image = new File(imagesFolder, "image_001.jpg");
Uri fileUri = Uri.fromFile(image);

i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

Upvotes: 1

CodingRat
CodingRat

Reputation: 1944

By default when you create an Android Virtual Devices (AVD) in Android, the Camera is disabled. So if your application uses Camera API, it might not work properly in Android Emulator. Also SDCard must be defined in emulator in order to use Camera. To enable Camera in your Android Emulator, just add following highlighted code in your AVD’s config.ini file. You can find the config.ini file under your user directory/.android folder.

in my case this is the path C:\Users\Gaurav.android\avd\GingerBread.avd

File: ~/.android/config.ini
hw.lcd.density=160
skin.name=HVGA
skin.path=platforms\android-9\skins\HVGA
hw.cpu.arch=arm
abi.type=armeabi
vm.heapSize=24
image.sysdir.1=platforms\android-9\images\
hw.camera=yes
sdcard.size=256M

or create a new AVD and enable camera while creating.

To capture and store image in you external directory you must specify storage permission to your app

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Upvotes: 0

Related Questions