I’m trying to have the user pick an audio file and store the path of that audio file. I’m doing so by using an ACTION_GET_CONTENT Intent so the user can pick an application to choose the file with. For some reason though, in the onActivityResult method, the Intent data is being returned as null, as is shown in the error message. Logcat error message and relevant code is given below. Any help would be greatly appreciated.
MAJOR EDIT:- This error only occurs when I press the back button or CANCEL ! Choosing a music file actually works fine. Sorry about that. Nonetheless, the error persists.
LogCat Error:-
06-26 16:36:25.238: E/AndroidRuntime(24759): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.packagename.appname.activityname}: java.lang.NullPointerException
Call to chooser activity placed in a method which is invoked in onCreate():-
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setAction(Intent.ACTION_PICK);
intent.setType("audio/*");
startActivityForResult(intent, audioreminder);
onActivityResult() method:-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
SharedPreferences flags = this.getSharedPreferences(
"screen_on_flags", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = flags.edit();
String FilePath = data.getData().getPath();
switch (requestCode) {
case audioreminder: {
if (resultCode == RESULT_OK) {
editor.putString("audioreminderpath", FilePath);
editor.commit();
} else if (resultCode == RESULT_CANCELED){
CheckBoxPreference other_audioreminder = (CheckBoxPreference) findPreference("other_audioreminder");
other_audioreminder.setChecked(false);
}
break;
}
}
}
I used, this code..
Also, in
onActivityResult()checkIntent dataeither is null or notTry this and let me know what happen..