I've seen several examples of this but they seem to be for older versions of the SDK. I'm trying to setup basic audio recording and the following code is giving me a NullPointerException
when targeting version 2.0 of the SDK.
ContentValues values = new ContentValues(2);
values.put(MediaStore.MediaColumns.TITLE, "somename");
values.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis());
ContentResolver resolver = getContentResolver();
Uri base = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
Uri newUri = resolver.insert(base, values);
I've narrowed it down to the last line as being what is throwing the exception. I've tested with the Log class and have found that the base
and values
variables are being set correctly.
Here is the exception output.
12-27 11:08:18.608: ERROR/DatabaseUtils(197): Writing exception to parcel
12-27 11:08:18.608: ERROR/DatabaseUtils(197): java.lang.NullPointerException
12-27 11:08:18.608: ERROR/DatabaseUtils(197): at com.android.providers.media.MediaProvider.insertInternal(MediaProvider.java:1478)
12-27 11:08:18.608: ERROR/DatabaseUtils(197): at com.android.providers.media.MediaProvider.insert(MediaProvider.java:1370)
12-27 11:08:18.608: ERROR/DatabaseUtils(197): at android.content.ContentProvider$Transport.insert(ContentProvider.java:150)
12-27 11:08:18.608: ERR开发者_运维技巧OR/DatabaseUtils(197): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:140)
12-27 11:08:18.608: ERROR/DatabaseUtils(197): at android.os.Binder.execTransact(Binder.java:287)
12-27 11:08:18.608: ERROR/DatabaseUtils(197): at dalvik.system.NativeStart.run(Native Method)
Also, in a lot of the examples I have seen, they add the mime type to the ContentValues
object using something like
values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType());
where the recorder variable is a MediaRecorder object. It appears however that the getMimeContentType()
method no longer exists. Could the insert be throwing the exception because I am not setting the mime type column? If so, How can I get the mime type in the new version of the sdk?
Thanks in advance!
EDIT:
I Think I found how to set the content mime type for the insert call. the line I came up with looks like
values.put(MediaStore.MediaColumns.MIME_TYPE, MediaStore.Audio.Media.CONTENT_TYPE);
I do however still get a NullPointerException
Oddly, I hadn't seen the audio recording example on the Android developers' site before, involving all the ContentProvider
stuff.
If all you need is to record audio to a file, then you don't need to do most of that; you can just set up the MediaRecorder
object, point it to a file and start recording:
// Prepare recorder source and type
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// File to which audio should be recorded
File outputFile = getFileStreamPath("output.amr");
Uri target = Uri.parse(outputFile.getAbsolutePath());
recorder.setOutputFile(target);
// Get ready!
recorder.prepare();
// Start recording
recorder.start();
// Stop and tidy up
recorder.stop();
recorder.release();
MediaStore NullPointerException ** SOLVED ** (partially).
Using Motodev Eclipse setup the default AVD was not set up to have a virtual SD card. To update this:
1) go to Run -> Run Configurations.
2) Find your appin in the list and select it.
3) On the right hand side select "Create new AVD"
4) Give it a name, for example AVDSD
5) In the SD Card section select "New" and put in a size over 9000 KB (min required).
6) I didn't make any changes to rest of the items.
Note: This solved the NullPointerException errors in the examples I was learning from, however there were no images on the AVD. It could be because I selected a virtual SD card. If you have an SD card on you computer you may point to it. Right now I don't but will try to get one.
Hope it helps.
精彩评论