用Zeros Android覆盖文件

时间:2017-05-13 19:40:05

标签: android

我试图用零覆盖一个文件,以便文件无法恢复..我已经搜索了很多,但没有什么安卓,所有我发现的是用于Windows ..所以我想从中选择w文件内部存储然后用零覆盖它。任何想法如何将被赞赏。

1 个答案:

答案 0 :(得分:2)

首先,将此权限添加到您的清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Make sure you request it at runtime if you target API 23+

然后,我们想写作:

File file = new File(context.getFilesDir(), filename);
String string = "000000000000000000000000000000000";//this can also be a randomly generated string of 0's. You can mix up numbers as well, or it can just be a static string.
FileOutputStream outputStream;

try {
  outputStream = new FileOutputStream(file);
  for(int i = 0; i < 100; i++)//Loop 100 times to write the 0's many times
      outputStream.write(string.getBytes());//And we write all the 0's to the file 100 times
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

这应该可以写很多0到目标文件。打开文件时,写入其中的新内容将覆盖当前的内容。然后循环100次(如果你愿意,可以删除)并将这些0写入文件100次。

要选择特定文件,您必须执行以下操作:(source

如果您希望用户能够选择系统中的任何文件,则需要包含您自己的文件管理器,或建议用户下载文件管理器。我相信你能做的最好的事情就是寻找&#34; openable&#34;像Intent.createChooser()这样的内容:

private static final int FILE_SELECT_CODE = 0;

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.", 
                Toast.LENGTH_SHORT).show();
    }
}

然后,您会在Uri中收听所选文件onActivityResult(),如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case FILE_SELECT_CODE:
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file 
            Uri uri = data.getData();
            Log.d(TAG, "File Uri: " + uri.toString());
            // Get the path
            String path = FileUtils.getPath(this, uri);
            Log.d(TAG, "File Path: " + path);
            // Get the file instance
            // File file = new File(path);
            // Initiate the upload
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

getPath()中的FileUtils.java方法是:

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
} 

最后,如果您实际上将内部存储视为应用本地私有存储区而不是设备的内部存储(用户称之为内部存储),请告诉我,以便我可以更新我的答案。外部存储适用于设备的内部存储(内部SD卡)和外部存储(通常是外部SD卡,但也可以是通过电源端口插入的USB存储设备。此处的外部存储不一定意味着SD卡,它基本上是指设备上所有应用的共享数据。如果您在应用内部存储中存储了某些内容,则其他应用和用户都无法访问它(除非设备已植根)。如果您在内部存储了某些内容SD卡,任何应用和用户都可以访问它。