1
나는 작은 프로젝트에 갇혀 있습니다. 전화를 잠글 때마다 이미지를 변경하기 위해 현재 잠금 화면 배경 무늬를 덮어 쓰려고합니다. 나는 API 23을 사용하여 Android Studio를 사용 중입니다. 휴대 전화에는 Android 6.0.1이 있으며 루팅되어 있습니다. 코드는 루트가 아닌 디렉토리에 파일을 복사합니다.안드로이드 액세스 내부 저장소 - 열지 못했습니다 : EACCES (권한이 거부되었습니다)
내 문제는, 데이터/데이터/... 내가 얻을
E/태그에 파일을 복사 할 때 : /data/data/com.sec.android.wallpapercropper2/files/를 wallpaper.png : 오픈 실패 : EACCES (사용 권한이 거부 됨)
내 작업에 수퍼 유저 권한을 부여하는 쉬운 방법이 있는지 궁금합니다. 검색하는 동안 찾은 유일한 방법은 셸 명령을 사용하여 수행하는 방법 이었지만 가능한 경우 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());
}
}