編輯:關於Android編程
這個類整合了寫文件,刪除文件,復制文件,搜索文件,判斷文件是否存在等,是對一些常用的功能進行封裝了。
用於記錄和方便以後使用。
[java]
public class FileAdapter {
private static final String TAG = "FileAdaptor";
/**
* 獲取指定位置的指定類型的文件
*
* @param path
* 文件夾路徑
* @param type
* 文件類型(如“*.jpg;*.png;*.gif”)
* @return
*/
public static void getFileList(String path, String type,
final OnFileListCallback onFileListCallback) {
new AsyncTask<String, String, String>() {
ArrayList<FileInfo> list = new ArrayList<FileInfo>();
@Override
protected void onPostExecute(String result) {
onFileListCallback.SearchFileListInfo(list);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String path = params[1].substring(params[1]
.lastIndexOf(".") + 1);
File file = new File(params[0]);
scanSDCard(file,path,list);
return null;
}
}.execute(path, type, "");
}
/**
* 掃描完成後的回調,獲取文件列表必須實現
*
* @author cola
*
*/
public interface OnFileListCallback {
/**
* 返回查詢的文件列表
* @param list 文件列表
*/
public void SearchFileListInfo(List<FileInfo> list);
}
private static void scanSDCard(File file, String ext, ArrayList<FileInfo> list) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File tmp = files[i];
if (tmp.isFile()) {
String fileName = tmp.getName();
String filePath = tmp.getName();
if (fileName.indexOf(".") >= 0) {
fileName = fileName.substring(fileName
.lastIndexOf(".") + 1);
if (ext != null && ext.equalsIgnoreCase(fileName)) {
AspLog.i(TAG, filePath);
FileInfo info = new FileInfo();
info.fileName = filePath;
info.filePath = tmp.getAbsolutePath();
list.add(info);
}
}
} else
scanSDCard(tmp, ext, list);
}
}
} else {
if (file.isFile()) {
String fileName = file.getName();
String filePath = file.getName();
if (fileName.indexOf(".") >= 0) {
fileName = fileName
.substring(fileName.lastIndexOf(".") + 1);
if (ext != null && ext.equalsIgnoreCase(fileName)) {
AspLog.i(TAG, filePath);
FileInfo info = new FileInfo();
info.fileName = filePath;
info.filePath = file.getAbsolutePath();
list.add(info);
}
}
}
}
}
/**
* 判斷
*/
public static boolean isSdCardExist() {
return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
/**
* 目錄相關
*/
public static String getSdCardRootDir() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
public static String getCacheDir(Context ctx) {
// /data/data/<package name>/cache
return ctx.getCacheDir().getPath() + "/";
}
public static String getFilesDir(Context ctx) {
// /data/data/<package name>/files
return ctx.getFilesDir().getPath() + "/";
}
public static String getSharedPrefDir(Context ctx) {
String path = "/data/data/com.timedee.calendar/shared_prefs/";
mkDir(path);
return path;
}
public static String getSdDataDir() {
return getSdCardRootDir() + "timedee/.data/";
}
public static String getBackupDir() {
return getSdCardRootDir() + "timedee/.backup/";
}
public static boolean mkDir(String path) {
File dir = new File(path);
boolean res = dir.mkdirs();
return res;
}
/**
* 文件操作相關
*/
public static boolean writeFile(String path, InputStream is) {
boolean result = false;
FileOutputStream os = null;
BufferedOutputStream bos = null;
try {
File file = new File(path);
os = new FileOutputStream(file, false);
bos = new BufferedOutputStream(os);
int readLen = 0;
byte[] buf = new byte[1024];
while ((readLen = is.read(buf)) != -1) {
bos.write(buf, 0, readLen);
}
bos.flush();
bos.close();
os.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static boolean writeTextFile(String path, String data) {
boolean result = false;
FileWriter fw = null;
try {
File file = new File(path);
fw = new FileWriter(file);
fw.write(data);
fw.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static InputStream readFile(String path) {
File file = new File(path);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (Exception e) {
}
return fis;
}
public static boolean CopyFile(String fromFile, String toFile) {
try {
InputStream fosfrom = new FileInputStream(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[4096];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
bt = null;
return true;
} catch (Exception ex) {
return false;
}
}
public static boolean CopyAssetFile(Context ctx, String fromFile, String toFile) {
try {
InputStream fosfrom = ctx.getAssets().open(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[4096];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
bt = null;
return true;
} catch (Exception ex) {
return false;
}
}
public static boolean deleteFile(String path) {
try {
File file = new File(path);
return file.delete();
} catch (Exception ex) {
return false;
}
}
public static boolean isFileExist(String path) {
try {
File file = new File(path);
return file.exists();
} catch (Exception ex) {
}
return false;
}
}
public class FileAdapter {
private static final String TAG = "FileAdaptor";
/**
* 獲取指定位置的指定類型的文件
*
* @param path
* 文件夾路徑
* @param type
* 文件類型(如“*.jpg;*.png;*.gif”)
* @return
*/
public static void getFileList(String path, String type,
final OnFileListCallback onFileListCallback) {
new AsyncTask<String, String, String>() {
ArrayList<FileInfo> list = new ArrayList<FileInfo>();
@Override
protected void onPostExecute(String result) {
onFileListCallback.SearchFileListInfo(list);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String path = params[1].substring(params[1]
.lastIndexOf(".") + 1);
File file = new File(params[0]);
scanSDCard(file,path,list);
return null;
}
}.execute(path, type, "");
}
/**
* 掃描完成後的回調,獲取文件列表必須實現
*
* @author cola
*
*/
public interface OnFileListCallback {
/**
* 返回查詢的文件列表
* @param list 文件列表
*/
public void SearchFileListInfo(List<FileInfo> list);
}
private static void scanSDCard(File file, String ext, ArrayList<FileInfo> list) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
File tmp = files[i];
if (tmp.isFile()) {
String fileName = tmp.getName();
String filePath = tmp.getName();
if (fileName.indexOf(".") >= 0) {
fileName = fileName.substring(fileName
.lastIndexOf(".") + 1);
if (ext != null && ext.equalsIgnoreCase(fileName)) {
AspLog.i(TAG, filePath);
FileInfo info = new FileInfo();
info.fileName = filePath;
info.filePath = tmp.getAbsolutePath();
list.add(info);
}
}
} else
scanSDCard(tmp, ext, list);
}
}
} else {
if (file.isFile()) {
String fileName = file.getName();
String filePath = file.getName();
if (fileName.indexOf(".") >= 0) {
fileName = fileName
.substring(fileName.lastIndexOf(".") + 1);
if (ext != null && ext.equalsIgnoreCase(fileName)) {
AspLog.i(TAG, filePath);
FileInfo info = new FileInfo();
info.fileName = filePath;
info.filePath = file.getAbsolutePath();
list.add(info);
}
}
}
}
}
/**
* 判斷
*/
public static boolean isSdCardExist() {
return Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
/**
* 目錄相關
*/
public static String getSdCardRootDir() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
public static String getCacheDir(Context ctx) {
// /data/data/<package name>/cache
return ctx.getCacheDir().getPath() + "/";
}
public static String getFilesDir(Context ctx) {
// /data/data/<package name>/files
return ctx.getFilesDir().getPath() + "/";
}
public static String getSharedPrefDir(Context ctx) {
String path = "/data/data/com.timedee.calendar/shared_prefs/";
mkDir(path);
return path;
}
public static String getSdDataDir() {
return getSdCardRootDir() + "timedee/.data/";
}
public static String getBackupDir() {
return getSdCardRootDir() + "timedee/.backup/";
}
public static boolean mkDir(String path) {
File dir = new File(path);
boolean res = dir.mkdirs();
return res;
}
/**
* 文件操作相關
*/
public static boolean writeFile(String path, InputStream is) {
boolean result = false;
FileOutputStream os = null;
BufferedOutputStream bos = null;
try {
File file = new File(path);
os = new FileOutputStream(file, false);
bos = new BufferedOutputStream(os);
int readLen = 0;
byte[] buf = new byte[1024];
while ((readLen = is.read(buf)) != -1) {
bos.write(buf, 0, readLen);
}
bos.flush();
bos.close();
os.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static boolean writeTextFile(String path, String data) {
boolean result = false;
FileWriter fw = null;
try {
File file = new File(path);
fw = new FileWriter(file);
fw.write(data);
fw.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static InputStream readFile(String path) {
File file = new File(path);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (Exception e) {
}
return fis;
}
public static boolean CopyFile(String fromFile, String toFile) {
try {
InputStream fosfrom = new FileInputStream(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[4096];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
bt = null;
return true;
} catch (Exception ex) {
return false;
}
}
public static boolean CopyAssetFile(Context ctx, String fromFile, String toFile) {
try {
InputStream fosfrom = ctx.getAssets().open(fromFile);
OutputStream fosto = new FileOutputStream(toFile);
byte bt[] = new byte[4096];
int c;
while ((c = fosfrom.read(bt)) > 0) {
fosto.write(bt, 0, c);
}
fosfrom.close();
fosto.close();
bt = null;
return true;
} catch (Exception ex) {
return false;
}
}
public static boolean deleteFile(String path) {
try {
File file = new File(path);
return file.delete();
} catch (Exception ex) {
return false;
}
}
public static boolean isFileExist(String path) {
try {
File file = new File(path);
return file.exists();
} catch (Exception ex) {
}
return false;
}
}
在我們的日常開發中自定義控件還是用的挺多的,設計師或者產品為了更好的漂亮,美觀,交互都會做一寫牛逼的ui效果圖,但是最後實現的還是我們程序員啊。所以說 自定義view你還
一、表格布局 TableLayout表格布局TableLayout以行列的形式管理子元素,每一行是一個TableRow布局對象,當然也可以是普通的View對象,Table
今天簡單講解一下PackageInstaller 文件路徑: 下面開始講解: 首先,我們說一下安裝apk的幾種方式,整體上可以分為2類,一類是有界面安裝,一
前言在Android開發過程中,我發現很多安卓源代碼裡應用了設計模式,比較常用的有適配器模式(各種adapter),建造者模式(Alert Dialog的構建)等等。雖然