在这个项目中,首先我们必须保存特定人员的电话号码和邮件ID。当单击图像按钮时,我们需要获取30秒的视频,它直接发送到该人设置的邮件ID自动。但是我们无法在单击按钮时获得这两个操作。在这里,我们使用两个按钮进行这两个进一步的操作。因此,请帮助我们在单击按钮时获得这两个操作。
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" />
登录.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" />
查看您的代码并假设对所有按钮的单独单击运行良好,这就是您必须做的,
-
将 button4.setOnClickListener 中的电子邮件代码 pre4sent 移动到一个单独的方法,比如 emailRecordng()
-
从 onActivityResult 调用此方法 在 *if(resultCode==RESULT_CANCELED)*
块。
这样,当您单击图像按钮时,它将首先为您录制语音,然后直接从您的onActivityResult()通过电子邮件发送
让我知道这是否有帮助