package 包名;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.os.Environment;
import android.util.Log;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
public class Check_File_Exist extends StandardFeature {
Activity activity;
String checkFileCallbackID;
IWebview webview;
// 页面传递参数(带后缀)
String filename = "";
@SuppressWarnings("unused")
public void checkFile(IWebview pWebview, JSONArray array) {
Log.i("checkFile--->", "成功进入检查文件插件");
activity = pWebview.getActivity();
checkFileCallbackID = array.optString(0);
webview = pWebview;
JSUtil.execCallback(webview, checkFileCallbackID, "ENTER_CHECK_FILE_CHAJIAN", JSUtil.OK, true);
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(array.optString(1));
filename = jsonArray.getString(0);
Log.i("filename-->", filename);
// 进行文件是否存在下载文件下的判断
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// 获取sdcard的读写成功
JSUtil.execCallback(webview, checkFileCallbackID, "GET_SDCARD_SUCCESS", JSUtil.OK, true);
// /storage/emulated/0
String path1 = Environment.getExternalStorageDirectory().toString();
// /data
String path2 = Environment.getDataDirectory().toString();
// 项目包名
String path3 = activity.getApplicationContext().getPackageName();
// /storage/emulated/0/Android/data/项目包名/downloads/文件名
String allPath = path1+"/Android" + path2 + "/" +path3 + "/downloads/" + filename;
Log.i("checkFile--当前拼接路径-->", allPath);
File files = new File(allPath);
//方法一 使用流去读大小
/*InputStream inputStream = null;
try {
inputStream = new FileInputStream(files);
int fileSize = inputStream.available();
JSUtil.execCallback(webview, checkFileCallbackID, "EXIST_FILE", JSUtil.OK, true);
} catch (FileNotFoundException e) {
// 未找到文件
JSUtil.execCallback(webview, checkFileCallbackID, "NOT_EXIST_FILE", JSUtil.OK, true);
e.printStackTrace();
} catch (IOException e) {
// 异常
JSUtil.execCallback(webview, checkFileCallbackID, "IO_EXCEPTION", JSUtil.OK, true);
e.printStackTrace();
}*/
//方法二 通过file判断
if(files.exists() && !files.isDirectory()) {
Log.i("checkFile-->", "文件存在且不为文件夹");
JSUtil.execCallback(webview, checkFileCallbackID, "EXIST_FILE", JSUtil.OK, true);
}else {
Log.i("checkFile-->", "文件不存在或格式为文件夹");
JSUtil.execCallback(webview, checkFileCallbackID, "NOT_EXIST_FILE", JSUtil.OK, true);
}
} else {
JSUtil.execCallback(webview, checkFileCallbackID, "GET_SDCARD_FAIL", JSUtil.OK, true);
}
} catch (JSONException e) {
// 传输数据格式异常
JSUtil.execCallback(webview, checkFileCallbackID, "JSON_EXCEPTION", JSUtil.OK, true);
e.printStackTrace();
}
}
}
查找指定文件夹下是否存在指定的文件名称(APP中根据安装后的地址再查询)