【发布时间】:2017-01-20 11:28:25
【问题描述】:
我被我的一个小项目困住了。 我想覆盖当前的锁屏壁纸,以便在每次锁定手机时更改其图像。我正在使用带有 API 23 的 android studio。 我的手机有 Android 6.0.1 并且已经植根。 该代码可以将文件复制到非根目录。
我的问题是,当试图将文件复制到 data/data/... 我得到
E/tag:/data/data/com.sec.android.wallpapercropper2/files/wallpaper.png:打开失败:EACCES(权限被拒绝)
我想知道是否有一种简单的方法可以为我的应用超级用户授予该任务的权限。我在搜索时发现的唯一一件事是如何使用 shell 命令执行此操作,但如果可能的话,我想使用 java 代码执行此操作。非常感谢!
public class MainActivity extends AppCompatActivity {
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verifyStoragePermissions(this);
copyFile("/storage/emulated/0/Download/", "wallpaper.png", "/data/data/com.sec.android.wallpapercropper2/files/");
}
private void copyFile(String inputPath, String inputFile, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
【问题讨论】:
标签: android