Video View Not Working with URI - android

When intent is fired for recording video on button click, it is recording that but in onActivityResult() method it is not playing the video.
Thanks in advance.
public class Main2Activity extends AppCompatActivity {
private Button button;
private VideoView videoView;
static final int REQUEST_VIDEO_RECORD = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
button = (Button) findViewById(R.id.record_video_button);
videoView = (VideoView) findViewById(R.id.display_video_view);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dispatchVideoIntent();
}
});
}
private void dispatchVideoIntent() {
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if(videoIntent.resolveActivity(getPackageManager()) != null){
startActivityForResult(videoIntent, REQUEST_VIDEO_RECORD);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_VIDEO_RECORD && resultCode == RESULT_OK){
Uri videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
}
}

Related

sd card photos not displayed when picking them from Image gallery , Android studio,

I am using this code to pick an image from gallery and display it in an imageView, the problem that this is working just for photos in internal storage, and when I pick an image from my sd card it's not displayed in my imageView
I hope you have a solution.
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
img.setImageURI(imageUri);
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
}
I am using those permissions in my manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I asked and I will answer, the problem was that most of my sd card photos are 2.0 MB or more and they can't be treated directly by the imageView so I added some lines in onActivityResult()
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
Bitmap bitmap ;
try {
bitmap=MediaStore.Images.Media
.getBitmap(this.getContentResolver(),imgUri);
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 160, 160, true);
img.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
I hope this will be helpful for those who can face the same problem

return back data to another activity

i have two activities A and B i am using a button to be directed to activity B i managed to write a code so it code be turned back to activity A automatically without any buttons after 5 seconds
but now i want when it return back to return data to A how it can be done
i wrote the code in A to recieve data but don't know what to add in activity B
Activity A ::
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button two = (Button) findViewById(R.id.button2);
Recieve = (TextView) findViewById(R.id.textView4);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent Intent = new Intent(MainActivity.this, Gps.class);
//startActivity( Intent);
sendMessage();
}
});
}
public void sendMessage() {
Intent intent = new Intent(MainActivity.this, Hello.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Response = data.getStringExtra("key");
Recieve.setText("msg is " + Response);
}
}
Activity B::
public class Hello extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent i = new Intent(Hello.this, MainActivity.class);
Hello.this.startActivity(i);
Hello.this.finish();
}
}, 5000);
}
}

Open video from gallery and Intent VideoView to new activity

I want make video player app that launch from gallery and play the video to 'new activity'. the problem is, i already browse the video at the gallery. the video are playing at the 'main activity' not play at the 'new activity'. please help.
MainActivity :
public class MainActivity extends Activity {
Button button1;
VideoView videoView;
private static final int SELECT_VIDEO = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
videoView=(VideoView)findViewById(R.id.videoView);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"), SELECT_VIDEO);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
if (requestCode == SELECT_VIDEO) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
}
}
}
what coding should i put in my 'new activity.java'?
and i also want to put 4 video in same activity. what should i do?
put the intent in NewActivity not in MainActivity

Text-to-speech from recognition result

I am using the TextToSpeech class in order to make the system speak.
Firstly I speak something through the voice recognizer and set the text to a textView. Next I want the system to speak what I've spoken earlier.
I find that the first step is working successfully but I don't hear anything after I speak.
My code is as follows:
public class MainActivity extends AppCompatActivity {
private int REQUEST_CODE=1;
TextView text;
ImageButton button;
TextToSpeech textToSpeech;
private Button ttsButton;
private ArrayList<String> results;
private String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
button=(ImageButton)findViewById(R.id.imageButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, REQUEST_CODE);
}
});
textToSpeech =new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS)
textToSpeech.setLanguage(Locale.US);
}
});
textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, null,null);
}
public void onPause(){
if(textToSpeech !=null){
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onPause();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
s=results.get(0);
text=(TextView)findViewById(R.id.text);
text.setText(""+results.get(0));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
You forgot to trigger TTS engine after activity result :-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
s=results.get(0);
text=(TextView)findViewById(R.id.text);
text.setText(""+results.get(0));
//Trigger text to speech here
textToSpeech.speak(results.get(0), TextToSpeech.QUEUE_FLUSH, null,null);
}
super.onActivityResult(requestCode, resultCode, data);
}

Display video in gallery in VideoView

My UI has a button and a videoview.When we click button,go to Gallery Video and when we select a video in Video gallery,it return my UI and videoview will display video that is selected..I used code as follow but it don't display video :(
public class VideoGalleryActivity extends Activity {
/** Called when the activity is first created. */
Button button;
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button);
videoView=(VideoView)findViewById(R.id.videoview);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
}) ;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
}
}
You have to let videoview start by adding videoview.start();. Code:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
videoview.start(); //edited
}
}

Categories

Resources