javaandroideclipseimport

Android DownloadManager not know where the file downloads


After two days of tests we were able to partially work.
I downloaded the file, but I have no idea where to store it.
I tried to download a picture and the picture appears in the folder /all_downloads

I do not know how to store it in /sdcard/update.

import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class Upgrade extends ActionBarActivity {
    private long enqueue;
    private DownloadManager dm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrade);

        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);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {


                        }
                    }
                }
            }
        };

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

    public void onClick(View view) {
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("http://android.vrt.ro/tv-update/v1.apk"));
        enqueue = dm.enqueue(request);


    }

    public void showDownload(View view) {

        Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(Uri.parse("file:///sdcard/download/v1.apk"),
                        "application/vnd.android.package-archive");
        startActivity(promptInstall);

    }

}

Solution

  • Use this

    request.setDestinationInExternalPublicDir("/updates", "update.apk");
    

    Added in API level 9

    public DownloadManager.Request setDestinationInExternalPublicDir (String dirType, String subPath)
    

    Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).

    The downloaded file is not scanned by MediaScanner. But it can be made scannable by calling allowScanningByMediaScanner().

    Parameters dirType the directory type to pass to getExternalStoragePublicDirectory(String)` subPath the path within the external directory, including the destination filename Returns this object Throws IllegalStateException If the external storage directory cannot be found or created.

    You can also use this version

    String updatePath = Environment.getExternalStorageDirectory() + File.separator + "Updates" + File.separator + "update.apk";
    request.setDestinationUri(Uri.fromFile(new File(updatePath)))