Android:由于某些权限,应用程序无法在SD卡上写入

时间:2015-07-21 16:08:30

标签: java android xml

m trying to encrypt a file from my SD card but i have some exception. How can i make sdcard read/write ? I已经在我的设备上的模拟器终端中尝试了一些linux命令,但是它们没有用。

这是我的例外

   07-21 19:03:12.453  31541-31541/encryption.nikola.com.test W/System.err﹕ java.io.FileNotFoundException: /encrypt.txt: open failed: EROFS (Read-only file system)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:458)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at encryption.nikola.com.test.Crypto.encryptor(Crypto.java:39)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at encryption.nikola.com.test.MainActivity.onActivityResult(MainActivity.java:60)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:5467)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3380)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2784)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2826)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-21 19:03:12.463  31541-31541/encryption.nikola.com.test W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
07-21 19:03:12.473  31541-31541/encryption.nikola.com.test W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
07-21 19:03:12.473  31541-31541/encryption.nikola.com.test W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-21 19:03:12.473  31541-31541/encryption.nikola.com.test W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
07-21 19:03:12.473  31541-31541/encryption.nikola.com.test W/System.err﹕ at libcore.io.Posix.open(Native Method)
07-21 19:03:12.473  31541-31541/encryption.nikola.com.test W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-21 19:03:12.473  31541-31541/encryption.nikola.com.test W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:442)

在这里,我将向您展示应用程序的代码

public class MainActivity extends Activity {
    final int ACTIVITY_CHOOSE_FILE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) this.findViewById(R.id.btnActivity1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent chooseFile;
                Intent intent;
                chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
                chooseFile.setType("file/*");
                intent = Intent.createChooser(chooseFile, "Choose a file");
                startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String filePath = null;
        switch(requestCode) {
            case ACTIVITY_CHOOSE_FILE: {
                if (resultCode == RESULT_OK){
                    Uri uri = data.getData();
                     filePath = uri.getPath();
                }
            }
        }

        Crypto crypto = new Crypto();
        crypto.encryptor(filePath);
    }


}

这是加密类

    public class Crypto {

        public void encryptor(String inputFilePath) {

            FileOutputStream fos = null;
            File file = new File(inputFilePath);
            String keyString = "140405PX_0.$88";
            String algorithm = "DESede";
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] fileByteArray = new byte[fileInputStream.available()];
                fileInputStream.read(fileByteArray);
                for (byte b : fileByteArray) {
                    System.out.println(b);
                }
                SecretKey secretKey = getKey(keyString);
                Cipher cipher = Cipher.getInstance(algorithm);
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(new CipherOutputStream(new FileOutputStream("encrypt.txt"), cipher));
                objectOutputStream.writeObject(fileByteArray);
                objectOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

public static SecretKey getKey(String message) throws Exception {
        String messageToUpperCase = message.toUpperCase();
        byte[] digestOfPassword = messageToUpperCase.getBytes();
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        return key;
    }

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="encryption.nikola.com.test" >

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

0 个答案:

没有答案