复制文件夹资产,内存中的文件时出错

时间:2015-03-08 20:16:02

标签: android

我希望程序执行时。

要存储在手机中的一些资产文件, 我使用以下方法:

   package com.example.copy_external;



public class MainActivity extends ActionBarActivity {

InputStream in;
 OutputStream ou;
 String path1 = Environment.getExternalStorageDirectory() + "/bbb" + "/" + "source";

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

    Copy_sqlite(path1);

   }





public void Copy_sqlite(String address) {

File f1=new File(Environment.getExternalStorageDirectory(),"bbb");
f1.mkdir();
final File f2= new File(address);

try {


        in = getBaseContext().getAssets().open("detail.db");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

if(!f2.exists())
{

    try {
        try {
            f2.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ou = new FileOutputStream(f2 );

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        CopyDB(in, ou);
        Toast.makeText(getBaseContext(), "copy", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}//end

public void CopyDB(InputStream in, OutputStream ou) throws IOException

{

byte []buffer=new byte[1024];
int length;

while((length=in.read(buffer))>0)
{

    try {
        ou.write(buffer, 0, length);

    } catch (IOException e) {
        // TODO Auto-generated catch block


        e.printStackTrace();
    }

    }
in.close();

ou.close();

  }


}

此应用程序适用于很多手机。但是 某些型号的手机会出现此错误。 我真的不知道是什么问题? 我很困惑。

logcat的:(详细)

  03-08 23:55:16.054: W/System.err(1403): java.io.IOException: Permission denied
  03-08 23:55:16.064: W/System.err(1403):   at java.io.File.createNewFileImpl(Native Method)
  03-08 23:55:16.064: W/System.err(1403):   at java.io.File.createNewFile(File.java:1115)
   03-08 23:55:16.064: W/System.err(1403):  at com.example.copy_external.MainActivity.Copy_sqlite(MainActivity.java:56)
   03-08 23:55:16.064: W/System.err(1403):  at com.example.copy_external.MainActivity.onCreate(MainActivity.java:28)
   03-08 23:55:16.064: W/System.err(1403):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    03-08 23:55:16.075: W/System.err(1403):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
    03-08 23:55:16.075: W/System.err(1403):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
     03-08 23:55:16.075: W/System.err(1403):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
     03-08 23:55:16.075: W/System.err(1403):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
     03-08 23:55:16.084: W/System.err(1403):    at android.os.Handler.dispatchMessage(Handler.java:99)
     03-08 23:55:16.084: W/System.err(1403):    at android.os.Looper.loop(Looper.java:126)
     03-08 23:55:16.084: W/System.err(1403):    at android.app.ActivityThread.main(ActivityThread.java:3997)
     03-08 23:55:16.118: W/System.err(1403):    at java.lang.reflect.Method.invokeNative(Native Method)
     03-08 23:55:16.118: W/System.err(1403):    at java.lang.reflect.Method.invoke(Method.java:491)
     03-08 23:55:16.118: W/System.err(1403):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
     03-08 23:55:16.118: W/System.err(1403):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
     03-08 23:55:16.118: W/System.err(1403):    at dalvik.system.NativeStart.main(Native Method)
     03-08 23:55:16.118: W/System.err(1403): java.io.FileNotFoundException: /mnt/sdcard/bbb/source (Permission denied)
     03-08 23:55:16.124: W/System.err(1403):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
    03-08 23:55:16.124: W/System.err(1403):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:239)
    03-08 23:55:16.124: W/System.err(1403):     at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
    03-08 23:55:16.124: W/System.err(1403):     at java.io.FileOutputStream.<init>(FileOutputStream.java:77)
   03-08 23:55:16.124: W/System.err(1403):  at com.example.copy_external.MainActivity.Copy_sqlite(MainActivity.java:61)
   03-08 23:55:16.124: W/System.err(1403):  at com.example.copy_external.MainActivity.onCreate(MainActivity.java:28)
    03-08 23:55:16.124: W/System.err(1403):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
   03-08 23:55:16.124: W/System.err(1403):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
    03-08 23:55:16.124: W/System.err(1403):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
    03-08 23:55:16.124: W/System.err(1403):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    03-08 23:55:16.124: W/System.err(1403):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
    03-08 23:55:16.134: W/System.err(1403):     at android.os.Handler.dispatchMessage(Handler.java:99)
   03-08 23:55:16.134: W/System.err(1403):  at android.os.Looper.loop(Looper.java:126)
   03-08 23:55:16.134: W/System.err(1403):  at android.app.ActivityThread.main(ActivityThread.java:3997)
   03-08 23:55:16.134: W/System.err(1403):  at java.lang.reflect.Method.invokeNative(Native Method)
   03-08 23:55:16.134: W/System.err(1403):  at java.lang.reflect.Method.invoke(Method.java:491)
   03-08 23:55:16.134: W/System.err(1403):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   03-08 23:55:16.134: W/System.err(1403):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  03-08 23:55:16.134: W/System.err(1403):   at dalvik.system.NativeStart.main(Native Method)
 03-08 23:55:16.144: D/AndroidRuntime(1403): Shutting down VM
  03-08 23:55:16.144: W/dalvikvm(1403): threadid=1: thread exiting with uncaught exception (group=0x40014760)
 03-08 23:55:16.154: E/AndroidRuntime(1403): FATAL EXCEPTION: main
 03-08 23:55:16.154: E/AndroidRuntime(1403): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.copy_external/com.example.copy_external.MainActivity}: java.lang.NullPointerException
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.os.Looper.loop(Looper.java:126)
   03-08 23:55:16.154: E/AndroidRuntime(1403):  at android.app.ActivityThread.main(ActivityThread.java:3997)
   03-08 23:55:16.154: E/AndroidRuntime(1403):  at java.lang.reflect.Method.invokeNative(Native Method)
   03-08 23:55:16.154: E/AndroidRuntime(1403):  at java.lang.reflect.Method.invoke(Method.java:491)
   03-08 23:55:16.154: E/AndroidRuntime(1403):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
   03-08 23:55:16.154: E/AndroidRuntime(1403):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at dalvik.system.NativeStart.main(Native Method)
    03-08 23:55:16.154: E/AndroidRuntime(1403): Caused by: java.lang.NullPointerException
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at com.example.copy_external.MainActivity.CopyDB(MainActivity.java:90)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at com.example.copy_external.MainActivity.Copy_sqlite(MainActivity.java:69)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at com.example.copy_external.MainActivity.onCreate(MainActivity.java:28)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
    03-08 23:55:16.154: E/AndroidRuntime(1403):     ... 11 more

0 个答案:

没有答案