我正嘗試將文件複製到來自內部移動存儲器的micro SD卡並獲取錯誤Eacces。我搜索了很多,但現在找到了明確的解決方案。可以或不可以複製非固定/固定的Kitkat設備。EACCES權限在寫入SD卡時在奇巧中被拒絕
這裏是我的代碼:
package jss.kitkatfile;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button copy;
copy = (Button) findViewById(R.id.Copy);
copy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
write();
} catch (IOException e) {
e.printStackTrace();
}
public void write()
throws IOException {
String dirpath = Environment.getExternalStorageDirectory() + "/WhatsApp/Media";
String filename = "A.mp4";
File file = new File(dirpath,filename);
System.out.println(file);
FileInputStream fileInputStream = new FileInputStream(file);
byte[] filelength = new byte[fileInputStream.available()];
fileInputStream.read(filelength);
//String dest = Environment.getExternalStorageDirectory() + "/Music";
String dest = "/storage/MicroSD/Android";
File outfile = new File(dest, filename);
FileOutputStream fileOutputStream = new FileOutputStream(outfile);
fileOutputStream.write(filelength);
}
}
如下錯誤:
java.io.FileNotFoundException: /storage/MicroSD/Android/A.mp4: open failed: EACCES (Permission denied)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:409)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
W/System.err: at jss.kitkatfile.MainActivity.write(MainActivity.java:69)
W/System.err: at jss.kitkatfile.MainActivity$1.onClick(MainActivity.java:27)
W/System.err: at android.view.View.performClick(View.java:4478)
W/System.err: at android.view.View$PerformClick.run(View.java:18698)
W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:149)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5257)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
W/System.err: Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:393)
W/System.err: ... 15 more
是的,增加了讀取和寫入權限。/storage/MicroSD/Android是MicroSD卡中要拷貝A.mp4的android文件夾的路徑。 –