简体   繁体   中英

File not found exception - when creating new file on sdcard in android

In my application, my requirement is need to install .APK file from assets folder, so that I am trying to copy the apk file from assets folder to sdcard, I get File Not Found Exception. these the following code:

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath();
String file_name = "ImageDownloading.apk";

AssetManager assetManager = getAssets();
try{
   InputStream input = new BufferedInputStream(assetManager.open(file_name));
   File path = new File(file_path);
     if(!path.exists()){
       path.mkdirs()
    }          
  File file = new File(path,file_name);            
  OutputStream output = new FileOutputStream(file);  // Here i get File Not Found Exception error.
  byte data[] = new byte[1024];
  int count;
   while ((count = input.read(data)) != -1) {
     output.write(data, 0, count);
    }
  output.flush();
  output.close();
  input.close();
    }
   catch(FileNotFoundException e){
  Toast.makeText(MainActivity.this,"File not found exception " + e.getMessage(),               Toast.LENGTH_SHORT).show();
     }

I have spent a lot of time but i did not find out the solution.

您是否在清单文件中设置了此权限?

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

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