簡體   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