下面列出了android.content.Context#getFileStreamPath ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public File release() throws IOException {
Context context = getContext();
int cpuType = Cpu.getCpuType();
final String cflagName;
if (cpuType == Cpu.CPU_INTEL){
cflagName = ShellAndroid.CFLAG_TOOL_X86_FILE_NAME;
}else{
cflagName = ShellAndroid.CFLAG_TOOL_FILE_NAME;
}
try {
AssetUtils.extractAsset(context, cflagName, true);
} catch (IOException e) {
// e.printStackTrace();
// extra cflag error, so don't block the sh
throw e;
}
File cFlag = context.getFileStreamPath(cflagName);
return cFlag;
}
/**
* initialize command terminal flag tool
* with exist cflag
* @param context
* @param cFlag a exist cflag
* @return
*/
public String initFlag(Context context, File cFlag){
File flagFile = context.getFileStreamPath(FLAG_FILE_NAME + FLAG_ID.incrementAndGet());
if (!flagFile.exists()) {
try {
flagFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
if (cFlag != null){
mFlagTrigger = cFlag.getAbsolutePath();
}else{
mIsInBlockMode = false;
}
mChmod.setChmod(mFlagTrigger, "777");
return flagFile.getAbsolutePath();
}
public static void install(Context context) {
if(instance == null) {
instance = new TxtOverlay(context.getApplicationContext());
File youyuan = context.getFileStreamPath("SIMYOU.ttf");
if (!youyuan.exists()){
AssetManager am = context.getAssets();
try {
InputStream is = am.open("zk/SIMYOU.ttf");
FileOutputStream os = context.openFileOutput("SIMYOU.ttf", Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void extractPlugin() {
Context context = ApplicationContextHolder.getContext();
try {
String[] fileNames = context.getAssets().list("plugin");
for (String file : fileNames) {
File extractFile = context.getFileStreamPath(file);
try (InputStream is = context.getAssets().open("plugin" + "/" + file);
FileOutputStream fos = new FileOutputStream(extractFile)) {
byte[] buffer = new byte[1024];
int count;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/** 删除手机内存中的文件*/
public static boolean deleteFile(Context context,String filename){
File file=context.getFileStreamPath(filename);
if(!file.exists()){
return true;
}else{
return file.delete();
}
}
private static void doPrivateDirFileFuzzing(MethodHookParam param, int index, FileFuzzingSerializableObject fuzzyingObject) {
String fileName = (String)param.args[index];
Context appContext = Hooker.applicationContext;
File localFile = appContext.getFileStreamPath(fileName);
//only create a dummy file if there is no file
if(!localFile.exists()) {
//what file format do we need?
copyCorrectFile(localFile, fuzzyingObject.getFileFormat());
}
}
/**
* 检查 files 目录下个某个文件是否已经为最新(比 assets 目录下的新)
*
* @param c
* @param filename
* @return 如果文件比 assets 下的同名文件的时间戳旧,或者文件不存在,则返回 false.
*/
public static boolean isFileUpdated(Context c, String filename) {
File file = c.getFileStreamPath(filename);
if (file == null) {
return false;
}
if (!file.exists()) {
return false;
}
long timestampOfFile = getFileTimestamp(c, filename);
long timestampOfAsset = getBundleTimestamp(c, filename);
return (timestampOfAsset <= timestampOfFile);
}
public static boolean isExtractedFromAssetsToFiles(Context c, String filename) {
File file = c.getFileStreamPath(filename);
if (file == null || !file.exists()) {
if (BuildConfig.DEBUG) {
Log.i(TAG, "Extract no exist file from assets filename = " + filename);
}
return true;
}
// compare file version for extract
return compareDataFileVersion(c, filename);
}
private File getFile(Context context) throws FileNotFoundException {
// check if data is an absolute path or a local app path and check if file exists:
File file = data.contains(File.separator) ? new File(data) : context.getFileStreamPath(data);
if (!file.exists()) {
throw new FileNotFoundException(String.format("File '%s' does not exist", data));
}
return file;
}
/**
* Reads data from the file in internal memory
*
* @param fileName File name
* @param context Context
* @return Read data
*/
public static String readInternalFile(String fileName, Context context) {
String dataFromFile = "";
File file = context.getFileStreamPath(fileName);
if (file.exists()) {
try {
InputStream inputStream = context.openFileInput(fileName);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
dataFromFile = stringBuilder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return dataFromFile;
}
/**
* Juget whether the cache file exists.
*
* @param context context
* @param cachefile cache file
* @return true if the cache data exists, false otherwise.
*/
public static boolean isExistDataCache(Context context, String cachefile) {
if (context == null)
return false;
boolean exist = false;
File data = context.getFileStreamPath(cachefile);
if (data.exists())
exist = true;
return exist;
}
/**
* Deletes the error log file.
* @param context
*/
public static void deleteErrorLogFile(Context context) {
File errorLog = context.getFileStreamPath(context.getString(R.string.error_log_file));
if (errorLog.exists()) {
//noinspection ResultOfMethodCallIgnored
errorLog.delete();
}
}
/** Function to use our version of BluetoothAdapter and BluetoothDevice if the native Android OS doesn't have 4.3 yet.
* @throws IOException
* @throws ClassNotFoundException **/
public static void loadSamsungLibraries(Context hostActivityContext) throws IOException, ClassNotFoundException{
File internalStoragePath = hostActivityContext.getFileStreamPath("com.samsung.ble.sdk-1.0.jar");
if (!internalStoragePath.exists()){
//We'll copy the SDK to disk so we can open the JAR file:
// it has to be first copied from asset resource to a storage location.
InputStream jar = hostActivityContext.getAssets().open("com.samsung.ble.sdk-1.0.jar", Context.MODE_PRIVATE);
FileOutputStream outputStream = hostActivityContext.openFileOutput("com.samsung.ble.sdk-1.0.jar", Context.MODE_PRIVATE);
int size = 0;
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[1024];
while((size=jar.read(buffer,0,1024))>=0){
outputStream.write(buffer,0,size);
}
jar.close();
}
Log.i(logTag, internalStoragePath.getAbsolutePath()+" exists? "+ internalStoragePath.exists());
URL[] urls = { new URL("jar:file:" + internalStoragePath.getAbsolutePath()+"!/") };
URLClassLoader cl = URLClassLoader.newInstance(urls);
samsungBluetoothAdapterClass = cl.loadClass("android.bluetooth.BluetoothAdapter");
samsungBluetoothDeviceClass = cl.loadClass("android.bluetooth.BluetoothDevice");
}
/**
* Create service.
*
* @param context this service runs in
*/
public UnreadNotificationsService(Context context) {
this.context = context;
this.persistFile = context.getFileStreamPath(persistFileName);
this.authenticationManager = AuthenticationManager.getInstance();
this.notificationColor = context.getResources().getColor(R.color.apptheme_colorPrimary);
createNotificationChannel();
}
public static boolean doesStorageFileExist(String key, Context context) {
File file = context.getFileStreamPath(key);
return file.exists();
}
public boolean deleteIdentityFile(Context con) {
File file = con.getFileStreamPath("sqrl.dat");
return file.delete();
}
private File getCuFile(Context context) {
if (cuFile == null)
cuFile = context.getFileStreamPath(cuFileName);
return cuFile;
}
private File getCuliFile(Context context) {
if (culiFile == null)
culiFile = context.getFileStreamPath(culiFileName);
return culiFile;
}
public static String readFile(Context context, String name) throws IOException{
File file = context.getFileStreamPath(name);
InputStream is = new FileInputStream(file);
byte b[] = new byte[(int) file.length()];
is.read(b);
is.close();
String string = new String(b);
return string;
}
/**
* Store donation timestamp.
*
* @param context
* @param timestamp to store
*/
public static void storeDonationTimestamp(Context context, Long timestamp) {
File file = context.getFileStreamPath(DTFN);
Utils.writeToStore(TAG, context, file, timestamp);
}