下面列出了android.content.Context#deleteFile ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Deletes all stack traces and meta files from files dir.
*
* @param context The context to use. Usually your Activity object.
*/
public static void deleteStackTraces(Context context) {
Log.d(ClientLog.TAG_MONITORING_CLIENT, "Looking for exceptions in: " + Constants.FILES_PATH);
String[] list = searchForStackTraces();
if ((list != null) && (list.length > 0)) {
Log.d(ClientLog.TAG_MONITORING_CLIENT, "Found " + list.length + " stacktrace(s).");
for (int index = 0; index < list.length; index++) {
String fileName = list[index];
try {
Log.d(ClientLog.TAG_MONITORING_CLIENT, "Delete stacktrace " + fileName + ".");
deleteStackTrace(context, list[index]);
context.deleteFile(list[index]);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Test
public void testWritePrivateFile() throws Exception {
final byte[] data = new byte[] {
0x1, 0x2, 0x3, 0x4, 0x8
};
final String fileName = "testFileForWrite";
final Context context = RuntimeEnvironment.application;
context.deleteFile(fileName);
FileUtils.writeOrDeletePrivateFile(context, fileName, data);
final InputStream is = context.openFileInput(fileName);
final byte[] read = ByteStreams.toByteArray(is);
assertArrayEquals(data, read);
}
/**
* Deletes the give filename and all corresponding files (same name,
* different extension).
*/
private static void deleteStackTrace(WeakReference<Context> weakContext, String filename) {
Context context = null;
if (weakContext != null) {
context = weakContext.get();
if (context != null) {
context.deleteFile(filename);
String user = filename.replace(".anr", ".user");
context.deleteFile(user);
String contact = filename.replace(".anr", ".contact");
context.deleteFile(contact);
String description = filename.replace(".anr", ".description");
context.deleteFile(description);
}
}
}
/**
* Upgrade table.
*
* @param db {@link SQLiteDatabase}
* @throws IOException IOException
*/
public static void onUpgrade(final Context context, final SQLiteDatabase db)
throws IOException {
Log.w(TAG, "Upgrading table: " + TABLE);
String fn = TABLE + ".bak";
context.deleteFile(fn);
ObjectOutputStream os = new ObjectOutputStream(context.openFileOutput(fn,
Context.MODE_PRIVATE));
backup(db, TABLE, PROJECTION, null, null, null, os);
os.close();
ObjectInputStream is = new ObjectInputStream(context.openFileInput(fn));
onCreate(db);
reload(db, TABLE, is);
is.close();
}
/**
* Upgrade table.
*
* @param db {@link SQLiteDatabase}
* @throws IOException IOException
*/
public static void onUpgrade(final Context context, final SQLiteDatabase db)
throws IOException {
Log.w(TAG, "Upgrading table: " + TABLE);
String fn = TABLE + ".bak";
context.deleteFile(fn);
ObjectOutputStream os = new ObjectOutputStream(context.openFileOutput(fn,
Context.MODE_PRIVATE));
backup(db, TABLE, PROJECTION, null, null, null, os);
os.close();
ObjectInputStream is = new ObjectInputStream(context.openFileInput(fn));
onCreate(db);
reload(db, TABLE, is);
is.close();
}
public void removeProfile(Context context, VpnProfile profile) {
String vpnentry = profile.getUUID().toString();
profiles.remove(vpnentry);
saveProfileList(context);
context.deleteFile(vpnentry + ".vp");
if (mLastConnectedVpn == profile)
mLastConnectedVpn = null;
}
/**
* Deletes the file in internal memory
*
* @param fileName File name
* @param context Context
*/
public static void deleteInternalFile(String fileName, Context context) {
try {
context.deleteFile(fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
@Before
public void setup() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
context.deleteFile("MapboxBlacklist");
setSystemPrefs();
ConfigurationClient configurationClient = new ConfigurationClient(context,
TelemetryUtils.createFullUserAgent("AnUserAgent", context), "anAccessToken", new OkHttpClient());
this.certificateBlacklist = new CertificateBlacklist(context, configurationClient);
}
public static void deleteAllApiFileCache(Context context) {
if (context != null) {
String[] fileNames = context.fileList();
if (!BaseTypeUtils.isArrayEmpty(fileNames)) {
synchronized (fileNames) {
for (String cacheName : fileNames) {
context.deleteFile(cacheName);
}
}
}
}
}
protected final String saveFile(Context context, Bitmap resource, int quality, Format format) throws ResourceCreationException {
FileOutputStream fos = null;
String fileName = UUID.randomUUID().toString();
String file = null;
try {
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
resource.compress(adaptFormat(format), quality, fos);
resource.recycle();
file = fileName;
} catch (java.io.FileNotFoundException e) {
throw new ResourceCreationException("Could not create new file");
} finally {
if (fos != null) {
try {
fos.close();
if (StringUtils.isBlank(file)) {
// failed, delete the file just in case it's there:
context.deleteFile(fileName);
}
} catch (IOException ignored) {
}
}
}
return file;
}
public void removeProfile(Context context, VpnProfile profile) {
String vpnentry = profile.getUUID().toString();
profiles.remove(vpnentry);
saveProfileList(context);
context.deleteFile(vpnentry + ".vp");
if (mLastConnectedVpn == profile)
mLastConnectedVpn = null;
}
static public void deleteGesture(Context context, String gestureName){
//delete the file from the local storage
context.deleteFile(gestureName + GESTURE_NAME_SUFFIX);
//remove the gesture pref
ApplicationPreferences.removeGesture(context,gestureName);
}
static void deleteStateForTest(Context context) {
context.deleteFile(STATE_FILENAME);
}
/** Deletes {@link #TICL_STATE_FILENAME}. */
public static void deleteStateFile(Context context) {
context.deleteFile(TICL_STATE_FILENAME);
}
/** Deletes {@link #TICL_STATE_FILENAME}. */
public static void deleteStateFile(Context context) {
context.deleteFile(TICL_STATE_FILENAME);
}
static void deleteStateForTest(Context context) {
context.deleteFile(STATE_FILENAME);
}
public static void deleteApiFileCache(Context context, String cacheName) {
if (context != null && !TextUtils.isEmpty(cacheName)) {
context.deleteFile(cacheName);
}
}
public static void resetValues(Context context, String[] filenames) {
for(String filename : filenames) {
context.deleteFile(filename);
}
}
/**
* Delete the given private file associated with this Context's application package.
*
* @param context {@link Context}
* @param name The name of the file to delete; can not contain path separators.
* @return true if the file was successfully deleted; else false.
* @since 1.0.1
*/
public static boolean deleteFile(Context context, String name) {
return context.deleteFile(name);
}
/**
* Delete file from persistent store.
*
* @param context
* @param file to delete
* @return true if really deleted
*/
public static boolean deleteFromStore(Context context, File file) {
return context.deleteFile(file.getName());
}