Android下载管理器 - 无法打开下载无文件或路径存在

时间:2016-04-07 20:50:37

标签: java android android-studio

我正在尝试使用Android的下载管理器下载文件。下载成功后,我想将文件的内容复制到另一个。但是,我无法将其作为FileInputStream打开。我收到以下错误:

04-07 15:41:49.830 31782-31782/midamcorp.com.burgerkingapp E/Exception: /my_downloads/7: open failed: ENOENT (No such file or directory)/my_downloads/7

以下是启动器活动的代码:

public class launch_activity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_activity);
        final DownloadManager dManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
// set download alarm
        Intent downloadIntent = new Intent(launch_activity.this, downloadReceiver.class);
        PendingIntent pending = PendingIntent.getBroadcast(launch_activity.this, 0, downloadIntent, 0);
final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        // cal.set(Calendar.HOUR_OF_DAY, 4);

        final AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis() + 1000, AlarmManager.INTERVAL_DAY, pending);


        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(downloadID);
                    Cursor cursor = dManager.query(query);
                    if(cursor.moveToFirst()) {
                        int colIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(colIndex)) {
                       Uri uri = dManager.getUriForDownloadedFile(downloadID);
                           File file = new File(uri.getPath());

                            try {
                                FileInputStream inputStream = new FileInputStream(file);
                                FileOutputStream fileOutputStream = context.openFileOutput(databaseHelper.DB_NAME, MODE_PRIVATE);
                                byte[] buffer = new byte[1024];
                                int read;

                                while ((read = inputStream.read(buffer)) > 0) {
                                   fileOutputStream.write(buffer, 0 , read);
                                }

                                fileOutputStream.close();
                                inputStream.close();

                            } catch (Exception ex) {
                            Log.e("Exception", ex.getMessage() + uri.getPath());
                            }
                            }



                        }
                    }
                }

        };

        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

File dbFile = new File(getFilesDir(), "bk.db");
        if(!dbFile.exists()) {
           try {
               InputStream localDB = getAssets().open("bk.db");
               OutputStream newDB = new FileOutputStream(dbFile);
               byte[] buffer = new byte[1024];
               int read;

               while ((read = localDB.read(buffer)) > 0) {
                   newDB.write(buffer, 0 , read);
               }

               localDB.close();
               newDB.close();

           } catch(IOException e) {
                Log.e(this.getClass().toString(), "IO Error!");
           }
        }

        SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
       if(!(preferences.contains("User"))) {
           editor.putString("User", "admin");

           editor.putString("Pass", "admin");
           editor.commit();
       }
    }

    protected void onStart() {
        super.onStart();
        SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);


        if (!(preferences.getBoolean("configured", false))) { // app has not yet been set-up
            Intent intent = new Intent(this, midamcorp.com.burgerkingapp.preferences.class);
            intent.putExtra("setUp", true);
            startActivity(intent);
            return;
        } else {
            Calendar cal = Calendar.getInstance();
            if (cal.get(Calendar.HOUR_OF_DAY) > 4 && cal.get(Calendar.HOUR_OF_DAY) < 10) // between 4 - 10 AM
            {

                Intent intent = new Intent(this, breakfastHome.class);
                startActivity(intent);
            } else if ((cal.get(Calendar.HOUR_OF_DAY) == 10 && cal.get(Calendar.MINUTE) < 28)) { // between 10 - 10:28 AM
                Intent intent = new Intent(this, breakfastHome.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(this, lunchHome.class);
                startActivity(intent);
            }
        }
    }

    protected void onResume() {
        super.onResume();
        this.onStart();
    }

}

和downloadReceiver活动:

public class downloadReceiver extends BroadcastReceiver {

    private Context context;

    @Override
    public void onReceive(Context c, Intent i) {
        DownloadManager manager = (DownloadManager) c.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://onedrive.live.com/redir?resid=A8EBCBA721A8A30D!8973&authkey=!AIj_ELxdgN7ZBsc&ithint=file%2cdb"));
manager.enqueue(request);

}

        }

此外,有没有办法在请求下载时设置文件名。

非常感谢!

0 个答案:

没有答案