marian
marian

Reputation: 287

Android: Camera crashes after clicking save

I have been developing an app with video recorder function. The app is working fine but after finish recording video and click 'Save' button the app crashes with message "Unfortunately, app has stopped". But the recorded video is saved in the folder as coded. Can someone help me to solve this issue as I cant find where is the error. My codings are as follows:

MainActivity.java:

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.text.SimpleDateFormat;

public class MainActivity extends Activity {

final static int REQUEST_VIDEO_CAPTURED = 1;
VideoView videoviewPlay;
private Uri fileUri;
public static final int MEDIA_TYPE_VIDEO = 2;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonRecording = (Button) findViewById(R.id.recording);



    buttonRecording.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new  Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
            // create a file to save the video
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

            // set the image file name
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
        }
    });
}

    /** Create a file Uri for saving an image or video */

private static Uri getOutputMediaFileUri(int type) {

    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * Create a File for saving an image or video
 */
private static File getOutputMediaFile(int type) {

    // Check that the SDCard is mounted
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Do This Video/");


    // Create the storage directory(Do This Video) if it does not exist
   if (!mediaStorageDir.exists()) {

        if (!mediaStorageDir.mkdirs()) {



            Log.d("Do This Video", "Failed to create directory MyCameraVideo.");
           return null;
        }
    }


    // Create a media file name

    // For unique file name appending current timeStamp with file name
    java.util.Date date = new java.util.Date();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(date.getTime());

    File mediaFile;

    if (type == MEDIA_TYPE_VIDEO) {

        // For unique video file name appending current timeStamp with file name
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");

    } else {
        return null;
    }

    return mediaFile;
}






@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(resultCode == RESULT_OK){
        if(requestCode == REQUEST_VIDEO_CAPTURED){
            fileUri = data.getData();
            Toast.makeText(MainActivity.this,
                    fileUri.getPath(),
                    Toast.LENGTH_LONG)
                    .show();
        }
    }else if(resultCode == RESULT_CANCELED){
        fileUri = null;
        Toast.makeText(MainActivity.this,
                "Cancelled!",
                Toast.LENGTH_LONG)
                .show();
    }
}
}

Any help would be much appreciated. Thank you.

logcat:

11-13 13:57:54.538  27336-27336/com.example.dothis.video I/SELinux﹕   Function: selinux_android_load_priority [0], There is no sepolicy file.
11-13 13:57:54.538  27336-27336/com.example.dothis.video I/SELinux﹕  Function: selinux_android_load_priority [1], There is no sepolicy version   file.
11-13 13:57:54.538  27336-27336/com.example.dothis.video I/SELinux﹕ Function: selinux_android_load_priority , priority version is  VE=GOOGLE_POLICY
11-13 13:57:54.538  27336-27336/com.example.dothis.video I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
11-13 13:57:54.543  27336-27336/com.example.dothis.video D/dalvikvm﹕   Late-enabling CheckJNI
11-13 13:57:54.693  27336-27336/com.example.dothis.video  D/TextLayoutCache﹕ Enable myanmar Zawgyi converter
11-13 13:57:54.723  27336-27336/com.example.dothis.video D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
11-13 13:57:54.723  27336-27336/com.example.dothis.video D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
11-13 13:57:54.733  27336-27336/com.example.dothis.video D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
11-13 13:57:54.738  27336-27336/com.example.dothis.video E/﹕ Device driver API match
Device driver API version: 23
User space API version: 23
11-13 13:57:54.738  27336-27336/com.example.dothis.video E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
11-13 13:57:54.813  27336-27336/com.example.dothis.video  D/OpenGLRenderer﹕ Enabling debug mode 0
11-13 13:57:54.833  27336-27336/com.example.dothis.video D/TextLayoutCache﹕ Enable myanmar Zawgyi converter
11-13 13:57:57.018  27336-27336/com.example.dothis.video W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
11-13 13:58:07.848  27336-27336/com.example.dothis.video D/AndroidRuntime﹕ Shutting down VM
11-13 13:58:07.848  27336-27336/com.example.dothis.video W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41e23c08)
11-13 13:58:07.853  27336-27336/com.example.dothis.video E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.dothis.video, PID: 27336
    java.lang.RuntimeException: Unable to resume activity  {com.example.dothis.video/com.example.dothis.video.AndroidVideoCapture}:   java.lang.RuntimeException: Failure delivering result ResultInfo{who=null,   request=200, result=-1, data=null} to activity   {com.example.dothis.video/com.example.dothis.video.AndroidVideoCapture}:   java.lang.NullPointerException
            at    android.app.ActivityThread.performResumeActivity(ActivityThread.java:3076)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3105)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
        at android.app.ActivityThread.access$1000(ActivityThread.java:175)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5603)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=null} to activity {com.example.dothis.video/com.example.dothis.video.AndroidVideoCapture}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3681)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3063)

            

Upvotes: 1

Views: 562

Answers (2)

Rohan Kandwal
Rohan Kandwal

Reputation: 9326

The problem is in the call fileUri = data.getData(); in onActivityResult function of your code. The call should be data.getExtras().get("data"); which will get the actual data passed in the intent.

Please see answer by David Wasser for more clarification.

Upvotes: 1

tiny sunlight
tiny sunlight

Reputation: 6251

You can't get path simplely by getPath() and you should check if data or data.getData is null.

   if(requestCode == REQUEST_VIDEO_CAPTURED){
      if(data == null || data.getData() ==null){
         //Log.e();
          return;
      }
      fileUri = data.getData();
       String filepath = uritofilpath(fileUri);

   }

getpath:

   public static String getPath(Uri uri,Context ctx) {
        String res = null;
        if(null==uri){
            return res;
        }
        if (uri != null && uri.toString().startsWith("file://")) {
            return uri.toString().substring("file://".length());
        }
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = ctx.getContentResolver().query(uri, proj, null, null, null);
        if(cursor!=null){
            if(cursor.moveToFirst()){
                try {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    res = cursor.getString(column_index);
                }catch (Exception ignored){
                }finally {
                    closeCursor(cursor);
                }
            }
        }
        closeCursor(cursor);
        return res;
    }

Upvotes: 1

Related Questions