how to automatically record 30sec video and send via email in a single button click?

混江龙づ霸主 提交于 2019-12-13 04:02:32

问题


In this project first we have to save phone no and mail ID of a particular person.And an image button is placed.when the Image button is clicked once we need to get 30sec video and it directly send to the set mail id of that person Automatically. but we can't get these two actions in a single button click. Here we are using two buttons for these two further actions. so please help us to get these two actions in a single button click..

package com.example.attack;

import java.io.File;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
ImageButton imageButton1;
 Button button1,button2,button4;
 TextView textView1,textView2,textView3;
 EditText editText1,editText2;
 private Uri fileUri;
 public static final int MEDIA_TYPE_VIDEO = 2;
 private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
 public static MainActivity ActivityContext =null; 
 public static TextView output;

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

    imageButton1=(ImageButton)findViewById(R.id.imageButton1);
    button1=(Button)findViewById(R.id.button1);
    button4=(Button)findViewById(R.id.button4);
    button4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("video/mp4");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
                          " editText2.getText().toString()" });
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "vid`enter code here`eo");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "message");
            Uri uri = Uri.fromFile(new File(Environment
                            .getExternalStorageDirectory(), "Pictures/Mycameravideos/VID.mp4"));
            emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
            emailIntent.setType("text/plain");
            startActivity(emailIntent);
        }
    });
    //button4=(Button)findViewById(R.id.button4);
    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final Dialog d=new Dialog(MainActivity.this);
            d.setContentView(R.layout.login);
            d.setTitle("my dialog");
        //  ImageView image=(ImageView)d.findViewById(R.id.imageView1);
            //TextView text=(TextView)d.findViewById(R.id.textView1);
            //Button ok=(Button)d.findViewById(R.id.button1);
            textView1=(TextView)d.findViewById(R.id.textView1);
            textView2=(TextView)d.findViewById(R.id.textView2);
            textView2=(TextView)d.findViewById(R.id.textView3);
            editText1=(EditText)d.findViewById(R.id.editText1);
            editText2=(EditText)d.findViewById(R.id.editText2);
        //  image.setImageResource(R.drawable.ic_launcher);
            textView1.setText("warning");
            button2=(Button)d.findViewById(R.id.button2);
            d.show();
            button2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    SharedPreferences sp=getSharedPreferences("shapre",MODE_PRIVATE);
                    Editor edt=sp.edit();
                    edt.putString("key1",editText1.getText().toString());
                    edt.putString("key2",editText2.getText().toString());

                    edt.commit();
                    d.dismiss();
                }
            });

        }
    });
    output = (TextView)findViewById(R.id.output);
    imageButton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,30);
              //  startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
                // create a file to save the video
                fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 

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

                // set the video image quality to high
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); 

                // start the Video Capture Intent
                startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
               // Intent i = new Intent(D_MainActivity.this,Email.class);
              //  startActivity(i); 


        }
    });


        }



  /** 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.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraVideo");


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

        if (! mediaStorageDir.mkdirs()){

            output.setText("Failed to create directory MyCameraVideo.");

            Toast.makeText(ActivityContext, "Failed to create directory MyCameraVideo.", 
                    Toast.LENGTH_LONG).show();

            Log.d("MyCameraVideo", "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" + ".mp4");

    } else {
        return null;
    }

    return mediaFile;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // After camera screen this code will excuted

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {

        if (resultCode == RESULT_CANCELED) {

            output.setText("Video File : " +data.getData());

            // Video captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Video saved to:" +
                     data.getData(), Toast.LENGTH_LONG).show();




        } else if (resultCode == RESULT_OK) {

            output.setText("User cancelled the video capture.");

            // User cancelled the video capture
            Toast.makeText(this, "User cancelled the video capture.", 
                    Toast.LENGTH_LONG).show();


        } else {

            output.setText("Video capture failed.");

            // Video capture failed, advise user
            Toast.makeText(this, "Video capture failed.", 
                    Toast.LENGTH_LONG).show();

        }
    }


    }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Bproject_MainActivity" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="84dp"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/imageButton1"
    android:layout_centerVertical="true"
    android:text="button" />

 <TextView 
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="heello"

/>

 <Button
     android:id="@+id/button4"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/button1"
     android:layout_below="@+id/button1"
     android:layout_marginTop="48dp"
     android:text="Button" />

login.xml

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="90dp"
    android:layout_marginTop="58dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="27dp"
    android:text="Phone No" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_alignParentRight="true"
    android:ems="10" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="36dp"
    android:text="Email Id" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_alignTop="@+id/textView2"
    android:ems="10" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="41dp"
    android:text="Save" />


回答1:


Looking at your code and assuming that the individual clicks to all the buttons are working well, here's what you have to do,

  1. Move the emailing code pre4sent in your button4.setOnClickListener to a seperate method, say emailRecordng()

  2. Call this method from your onActivityResult in the *if(resultCode==RESULT_CANCELED)*
    block.

This way, when you click the imageButton, it will first record the voice for you and then it will be directly sent via the email from your onActivityResult()

Let me know if this helps



来源:https://stackoverflow.com/questions/22140395/how-to-automatically-record-30sec-video-and-send-via-email-in-a-single-button-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!