I am using Android studio to make a small App. In one screen, there is a button (button 1) is used to enable the cell phone to start record video. And after recording an AlertDialog is used to replay the video. However, every time when I run on emulator, the app just automatically stopped. Java script of this screen is copied here:
import static android.provider.MediaStore.*;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;
public class Screen3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen3);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
Intent intent1 = new Intent(ACTION_VIDEO_CAPTURE);
intent1.putExtra(EXTRA_DURATION_LIMIT, 5);
startActivityForResult(intent1, 1);
}
}
);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Screen3.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onActivityResult (int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
VideoView v1 = new VideoView(this);
v1.setVideoURI(data.getData());
v1.start();
builder.setView(v1).show();
}
}
}
And here is my manifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.cse535projectpart1">
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CSE535ProjectPart1"
tools:targetApi="31">
<activity
android:name=".Screen3"
android:exported="false" />
<activity
android:name=".Screen2"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Hopefully someone can give some suggestions how to resolve described issue. Thank you !