繁体   English   中英

文件未找到异常 - 在android中的sdcard上创建新文件时

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

在我的应用程序中,我的需求是需要从assets文件夹安装.APK文件,所以我试图将apk文件从assets文件夹复制到sdcard,我得到File Not Found Exception. 这些代码如下:

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();
     }

我花了很多时间,但我没有找到解决方案。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM