1.视频文件的删除
方法1:
在此文件LINUX-android-packages-apps-SnapdragonGallery-src-com-android-gallery3d-app-MovieActivity.java中回调函数的实现。
private void onDeleteMenuItemClick() {
Log.v(TAG, "zmy-onDeleteMenuItemClick()" );
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("Delete");
builder.setMessage("The item will be deleted");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.v(TAG, "zmy-onDeleteMenuItemClick()" );
MovieActivity.this.getContentResolver().delete(mMovieItem.getUri(), null, null);
MovieActivity.this.getContentResolver().update(mMovieItem.getUri(), null, null, null);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
方法2:Intent中URI的传递和解析
此MovieActivity.java跳转到DialogActivity.java。
LINUX-android-packages-apps-SnapdragonGallery-src-com-android-gallery3d-app-MovieActivity.java
private void onDeleteMenuItemClick() {
Intent intent = new Intent();
intent.setClass(MovieActivity.this, DialogActivity.class);
intent.putExtra("dMovieItem", mMovieItem.getUri().toString());//把URI转化成string后传递
startActivity(intent);
MovieActivity.this.finish();
}
在DialogActivity.java中
package com.android.gallery3d.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.codeaurora.gallery.R;
import android.view.KeyEvent;
import android.util.Log;
import android.net.Uri;
import android.content.Intent;
import android.content.IntentFilter;
import com.android.gallery3d.app.MovieActivity;
import org.codeaurora.gallery3d.ext.IMovieItem;
public class DialogActivity extends Activity
{
private static final String TAG = "DialogActivity";
private static final boolean LOG = true;
public static Uri dMovieItemUri;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);//增加dialog.xml布局文件
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
// button1.setVisibility(View.INVISIBLE);
// button2.setVisibility(View.GONE);
Intent intent = getIntent();
dMovieItemUri = Uri.parse(intent.getStringExtra("dMovieItem")); //解析视频文件的URI
button1.setOnClickListener(new Button.OnClickListener() { //取消按钮
public void onClick(View v)
{
DialogActivity.this.finish();
}
});
button2.setOnClickListener(new Button.OnClickListener() {//删除按钮
public void onClick(View v)
{
DialogActivity.this.getContentResolver().delete(dMovieItemUri, null, null);
DialogActivity.this.getContentResolver().update(dMovieItemUri, null, null, null);
DialogActivity.this.finish();
}
});
}
/* @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.v(TAG, "zmy-Dialog-keyCode ="+keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
Log.v(TAG, "zmy-Dialog-KEYCODE_ENTER");
DialogActivity.this.getContentResolver().delete(dMovieItemUri, null, null);
DialogActivity.this.getContentResolver().update(dMovieItemUri, null, null, null);
DialogActivity.this.finish();
return true;
case KeyEvent.KEYCODE_BACK:
Log.v(TAG, "zmy-Dialog-KEYCODE_BACK");
DialogActivity.this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}*/ //功能机则实现按键回调函数
public void onStart()
{
super.onStart();
Log.v(TAG, "onStart");
}
public void onResume()
{
super.onResume();
Log.v(TAG, "onResume");
}
public void onPause()
{
super.onPause();
Log.v(TAG, "onPause");
}
public void onStop()
{
super.onStop();
Log.v(TAG, "onStop");
}
public void onDestroy()
{
super.onDestroy();
Log.v(TAG, "onDestroy");
}
public void onRestart()
{
super.onRestart();
Log.v(TAG, "onReStart");
}
}
2.视频文件详情的实现
private void onDetailsMenuItemClick() {
Log.v(TAG, "zmy-onDetailsMenuItemClick()-mMovieItem.getUri() =" + mMovieItem.getUri() + "mMovieItem =" + mMovieItem );
Cursor cursor = this.getContentResolver().query(mMovieItem.getUri(), null, null, null, null);
cursor.moveToNext();
String m_title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
//String m_time = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.YEAR));
String m_size = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
String m_patch = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
String m_duration = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
cursor.close();
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("Details");
builder.setMessage("Title:" + m_title + "\n" + "Duration: " + m_duration + "\n" + "Path: " + m_patch + "\n" + "File Size: " + m_size );
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.create().show();
}
优化时间显示格式
private void onDetailsMenuItemClick() {
//zmy-display Details Dialog begin
Log.v(TAG, "zmy-onDetailsMenuItemClick()-mMovieItem.getUri() =" + mMovieItem.getUri() + "mMovieItem =" + mMovieItem );
Cursor cursor = this.getContentResolver().query(mMovieItem.getUri(), null, null, null, null);
cursor.moveToNext();
String m_title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
String m_time = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN));
long l_time =Long.parseLong(m_time);
Date date = new Date(l_time);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
String m_size = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
String m_patch = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
String m_duration = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
long l_size =Long.parseLong(m_size);
int l_duration =Integer.parseInt(m_duration);
double d_size = l_size/1024;
d_size = d_size/1024;
d_size = (double)Math.round(d_size*100)/100;
l_duration = l_duration/1000;
String f_duration = secToTime(l_duration);
cursor.close();
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("Details");
builder.setMessage("Title: " + m_title + "\n" + "Time: " + dateString + "\n" + "Duration: " + f_duration + "\n" + "Path: " + m_patch + "\n" + "File Size: " + d_size + "M" );
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.create().show();
//zmy-display Details Dialog end
}
public String secToTime(int time) {
String timeStr = null;
int hour = 0;
int minute = 0;
int second = 0;
if (time <= 0)
return "00:00:00";
else {
minute = time / 60;
if (minute < 60) {
second = time % 60;
timeStr = "00:"+unitFormat(minute) + ":" + unitFormat(second);
} else {
hour = minute / 60;
if (hour > 99)
return "99:59:59";
minute = minute % 60;
second = time - hour * 3600 - minute * 60;
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
}
}
return timeStr;
}