2017-01-20 26 views
1

私は小さなプロジェクトで立ち往生しています。 私は電話をロックするたびに画像を変更するために、現在のロック画面の壁紙を上書きします。私は、API 23でアンドロイドスタジオを使用しています。 私の携帯電話はAndroid 6.0.1を搭載しており、根付いています。 コードは、ファイルをルート以外のディレクトリにコピーします。Androidのアクセス内部ストレージ - オープンに失敗しました: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()); 
    } 
} 

答えて

0

私はrootTools

CommandCapture command = new CommandCapture(0, "cp -f " + old_file + " " + new_file); 
    try { 
    RootTools.getShell(true).add(command); 
    } catch(IOException io){} 
    catch(TimeoutException ti){} 
    catch(RootDeniedException rd){} 
の助けを借りて、それを解決するために管理
関連する問題