简体   繁体   中英

Can't modify file in external storage in android

Please help me... I'm getting

java.io.FileNotFoundException : /storage/'my external storage'/Music/'file name' : open failed: EACCES (Permission denied)

when I'm modifying music's id3 tags which are located in my sd card with jaudiotagger.

I already wrote read/write permissions on manifest file, and I wrote requesting method, permission request result callback method, and a part that modify tags when button clicked like this.

private void requestExternalStoragePermissions() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "Permission granted to write your External storage", Toast.LENGTH_SHORT).show();
            start();
        }
        else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1 : {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0&&grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
                start();
            }
            else {
                Toast.makeText(this, "Permission denied to access your External storage", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}

savebutton.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View view){
            try{
                AudioFile f = AudioFileIO.read(file);
                Tag tag = f.getTag();
                tag.setField(FieldKey.TITLE,editText1.getText().toString());
                tag.setField(FieldKey.ALBUM,editText2.getText().toString());
                tag.setField(FieldKey.ARTIST,editText3.getText().toString());
                tag.setField(FieldKey.ALBUM_ARTIST,editText4.getText().toString());
                tag.setField(FieldKey.YEAR,editText5.getText().toString());
                tag.setField(FieldKey.DISC_NO,editText6.getText().toString());
                tag.setField(FieldKey.TRACK,editText7.getText().toString());
                tag.setField(FieldKey.LYRICS,editText8.getText().toString());
                f.commit();
                Toast.makeText(getApplicationContext(), "Tag Saved", Toast.LENGTH_SHORT).show();
            } catch(Exception e){
                e.printStackTrace();
            }
        }
    });

How can I fix this?

In Android 4.4 and higher, editing files on the sd card is not possible anymore. I had the same issue..

The only workaround is to copy the file to the internal storage, modify it and move it back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM