权限被拒绝用USB笔式驱动器写入

时间:2014-03-10 11:05:55

标签: java android

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abc2"
    android:versionCode="1"
    android:versionName="1.1.8" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我试图将应用程序的数据库复制到USB pendrive,上次使用SD卡,它工作正常,但在我将SD卡路径更改为/mnt/sda/sda2后,它被拒绝错误,{{ 1}}是USB pendrive路径

这是将数据库复制到pendrive的复制文件功能

/mnt/sda/sda2

这就是我调用copyfile函数的方法

private void copyFile(String inputPath, String inputFile, String outputPath) {

        InputStream in = null;
        OutputStream out = null;
        try {

            //create output directory if it doesn't exist
            File dir = new File (outputPath); 
            if (!dir.exists())
            {
                dir.mkdirs();
            }


            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;  
            Toast.makeText(UltilityActivity.this, "Export successful!", Toast.LENGTH_SHORT).show();

        }  catch (FileNotFoundException fnfe1) {
            Log.e("tag", fnfe1.getMessage());
            Toast.makeText(UltilityActivity.this, "Export failed", Toast.LENGTH_SHORT).show();
        }
                catch (Exception e) {
            Log.e("tag", e.getMessage());
            Toast.makeText(UltilityActivity.this, "Export failed", Toast.LENGTH_SHORT).show();
        }

    }    
}

最后一次复制到SD卡,使用此代码

copyFile("/data/data/com.example.abc2/databases/","DB_BusData","/mnt/sda/sda2/");

复制到SD卡正在工作,但USB pendrive错误与: -

copyFile("/data/data/com.example.abc2/databases/","DB_BusData","/mnt/sdcard/");

是否需要安装USB驱动器?我能以编程方式安装吗?怎么样?请举个例子?

1 个答案:

答案 0 :(得分:0)

在Android操作系统中,通常USB pendrive会被称为SD卡,
但是,Androidmanifest.xml中的权限需要声明不同的权限, USB外部存储需要媒体启用权限,如下所示: -

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