下载的文件缺少扩展名

时间:2014-12-20 17:55:08

标签: android

我访问网站下载了一些文件但是一旦下载文件,就会丢失扩展名(.zip等)。 Project API 21(Android 5.0.1)。

private void execute(final Context context) {
    Intent service = null;
    while ((service = queue.peek()) != null) {
        if (running == null) {
            service = queue.remove();

            final Bundle extras = service.getExtras();
            final String url = extras.getString(DownloaderService.KEY_URL);
            final String title = extras.getString(DownloaderService.KEY_TITLE);
            final String filename = url.substring(url.lastIndexOf('/') + 1);

            final File parentDir = new File(Constants.WEB_DIR, title);
            if (!parentDir.exists())
                parentDir.mkdirs();
            final File file = new File(parentDir, (title == null ? filename : title));
            //Log.d("OUTPUTFILE: ", parentDir + "/" + filename); 
            final ProgressNotification notification = new ProgressNotification(context, file);
            notification.createWaiting(title == null ? filename : title);

            context.startService(service);
            running = service;
            Log.d(Constants.LOG, "download started");
            Log.d(Constants.LOG, "running downloads: " + running);
            break;
        }
    }
}

有人可以帮帮我吗?

目录和文件在那里,我可以使用Android安装程序执行该文件,但只缺少文件扩展名。

2 个答案:

答案 0 :(得分:0)

您是否查看了服务器从您检索文件的位置发送的mime类型?

如果原始存储机制不包含带存储的mimetype,则它可能被强制为应用程序/八位字节流 - 认为它是二进制数据。这是在s3中发生的事情,例如当您上传没有设置mimetype属性的文件时 - 或者您在上传过程中以某种方式将其搞砸了。

当您访问内容uri时,您将获得一个可能被下载系统解释为不明确的文件。当mime类型不明确时,这会导致各种问题,例如mime type map返回空字符串作为扩展名。

Good information about mime types and their configurations.

You could also add the step to guess the mime type from the stream - 作为保障措施。

答案 1 :(得分:0)

我自己解决了这个问题,这个错误在其他地方,这是正确的代码:

@Override
public void onStart(final Intent intent, final int startId) {
    final Bundle extras = intent.getExtras();
    final String username = extras.getString(KEY_USERNAME);
    final String password = extras.getString(KEY_PASSWORD);
    final String url = extras.getString(DownloaderService.KEY_URL);
    final String title = extras.getString(KEY_TITLE);
    final String filename = url.substring(url.lastIndexOf('/') + 1);

    final File parentDir = new File(Constants.WEB_DIR title);
    if (!parentDir.exists())
        parentDir.mkdirs();
    final File file = new File(parentDir + "/" + filename); <---- solved
    notification = new ProgressNotification(DownloaderService.this, file);
    super.onStart(intent, startId);

    new Thread() {

        @Override
        public void run() {
            try {
                download();
            } catch (final Exception e) {
                Log.e(Constants.LOG, e.getMessage(), e);
                parentDir.delete();
            }
            stopSelf();
        }

但是那些让我走上正轨的人。

相关问题