一、更新日志
1. 新增:显示SDCard剩余空间
如果SD卡容量有限,而且经常使用下载视频,对剩余空间比较关注。如图:
代码网上找的,也有在项目里用过的:
public static String showFileSize(long size) {
String fileSize;
if (size < KB)
fileSize = size + "B";
else if (size < MB)
fileSize = String.format("%.1f", size / KB) + "KB";
else if (size < GB)
fileSize = String.format("%.1f", size / MB) + "MB";
else
fileSize = String.format("%.1f", size / GB) + "GB";
return fileSize;
}
/** 显示SD卡剩余空间 */
public static String showFileAvailable() {
String result = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
long blockSize = sf.getBlockSize();
long blockCount = sf.getBlockCount();
long availCount = sf.getAvailableBlocks();
return showFileSize(availCount * blockSize) + " / " + showFileSize(blockSize * blockCount);
}
return result;
String fileSize;
if (size < KB)
fileSize = size + "B";
else if (size < MB)
fileSize = String.format("%.1f", size / KB) + "KB";
else if (size < GB)
fileSize = String.format("%.1f", size / MB) + "MB";
else
fileSize = String.format("%.1f", size / GB) + "GB";
return fileSize;
}
/** 显示SD卡剩余空间 */
public static String showFileAvailable() {
String result = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
long blockSize = sf.getBlockSize();
long blockCount = sf.getBlockCount();
long availCount = sf.getAvailableBlocks();
return showFileSize(availCount * blockSize) + " / " + showFileSize(blockSize * blockCount);
}
return result;
2. 改进:加载网页时显示正在加载效果
未改之前每次进入网页都会空白一片,加载完后才显示出来,体验较差。
3. 改进:在线视频栏目在多个网站之间切换时后退混乱的问题
未改前使用WebView默认的前进后退功能,使用中发现切换后还是会出现后退混乱的状况。注意clearHistory不能清除当前的url。
修改参照帖子,这里。 注意需要判断链接是否相同,即:
if (!mHistory.contains(url))
mHistory.add(0, url);
mHistory.add(0, url);
4. 新增:显示正在加载的url
5. 新增:删除、重命名视频文件
基础功能。
/** 删除文件 */
private void deleteFile(final FileAdapter adapter, final PFile f, final int position) {
new AlertDialog.Builder(getActivity()).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.file_delete).setMessage(getString(R.string.file_delete_confirm, f.title)).setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
File file = new File(f.path);
if (file.canRead() && file.exists())
file.delete();
FileBusiness.deleteFile(getActivity(), f);
adapter.delete(position);
} catch (Exception e) {
}
}
}).setPositiveButton(android.R.string.no, null).show();
}
/** 重命名文件 */
private void renameFile(final FileAdapter adapter, final PFile f, final int position) {
final EditText et = new EditText(getActivity());
et.setText(f.title);
new AlertDialog.Builder(getActivity()).setTitle(R.string.file_rename).setIcon(android.R.drawable.ic_dialog_info).setView(et).setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = et.getText().toString().trim();
if (name == null || name.trim().equals("") || name.trim().equals(f.title))
return;
try {
File fromFile = new File(f.path);
File nf = new File(fromFile.getParent(), name.trim());
if (nf.exists()) {
Toast.makeText(getActivity(), R.string.file_rename_exists, Toast.LENGTH_LONG).show();
} else if (fromFile.renameTo(nf)) {
f.title = name;
f.path = nf.getPath();
FileBusiness.renameFile(getActivity(), f);
adapter.notifyDataSetChanged();
}
} catch (SecurityException se) {
Toast.makeText(getActivity(), R.string.file_rename_failed, Toast.LENGTH_LONG).show();
}
}
}).setPositiveButton(android.R.string.no, null).show();
private void deleteFile(final FileAdapter adapter, final PFile f, final int position) {
new AlertDialog.Builder(getActivity()).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.file_delete).setMessage(getString(R.string.file_delete_confirm, f.title)).setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
File file = new File(f.path);
if (file.canRead() && file.exists())
file.delete();
FileBusiness.deleteFile(getActivity(), f);
adapter.delete(position);
} catch (Exception e) {
}
}
}).setPositiveButton(android.R.string.no, null).show();
}
/** 重命名文件 */
private void renameFile(final FileAdapter adapter, final PFile f, final int position) {
final EditText et = new EditText(getActivity());
et.setText(f.title);
new AlertDialog.Builder(getActivity()).setTitle(R.string.file_rename).setIcon(android.R.drawable.ic_dialog_info).setView(et).setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = et.getText().toString().trim();
if (name == null || name.trim().equals("") || name.trim().equals(f.title))
return;
try {
File fromFile = new File(f.path);
File nf = new File(fromFile.getParent(), name.trim());
if (nf.exists()) {
Toast.makeText(getActivity(), R.string.file_rename_exists, Toast.LENGTH_LONG).show();
} else if (fromFile.renameTo(nf)) {
f.title = name;
f.path = nf.getPath();
FileBusiness.renameFile(getActivity(), f);
adapter.notifyDataSetChanged();
}
} catch (SecurityException se) {
Toast.makeText(getActivity(), R.string.file_rename_failed, Toast.LENGTH_LONG).show();
}
}
}).setPositiveButton(android.R.string.no, null).show();
6. 修正:使用WebView播放视频时无法关闭的问题
使用浏览器的flash插件播放视频时可能会有此问题。
7. 改进:下载视频显示对应视频标题名称
未改前下载视频为一长串数字。
三、下载
至本章节往后,代码均不再提供下载,请移步Google Code:http://code.google.com/p/android-oplayer
无法访问请尝试地址:https://over140@android-oplayer.googlecode.com/hg/ ,参照这里。
本文转自博客园农民伯伯的博客,原文链接:使用Vitamio打造自己的Android万能播放器(8)——细节优化,如需转载请自行联系原博主。