Search in sources :

Example 51 with DownloadManager

use of android.app.DownloadManager in project android_frameworks_base by ResurrectionRemix.

the class FilesActivityUiTest method testDownload_RetryUnsuccessful.

@Suppress
public void testDownload_RetryUnsuccessful() throws Exception {
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    // This downloads fails! But it'll still show up.
    dm.enqueue(new Request(Uri.parse("http://www.google.com/hamfancy")));
    bots.roots.openRoot("Downloads");
    UiObject doc = bots.directory.findDocument("Unsuccessful");
    doc.waitForExists(TIMEOUT);
    int toolType = Configurator.getInstance().getToolType();
    Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_FINGER);
    doc.click();
    Configurator.getInstance().setToolType(toolType);
    assertTrue(bots.main.findDownloadRetryDialog().exists());
    // to clear the dialog.
    device.pressBack();
}
Also used : UiObject(android.support.test.uiautomator.UiObject) Request(android.app.DownloadManager.Request) DownloadManager(android.app.DownloadManager) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 52 with DownloadManager

use of android.app.DownloadManager in project Tusky by Vavassor.

the class ViewMediaFragment method downloadImage.

private void downloadImage() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && ContextCompat.checkSelfPermission(this.getContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        android.support.v4.app.ActivityCompat.requestPermissions(getActivity(), new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
    } else {
        //download stuff
        String url = getArguments().getString("url");
        Uri uri = Uri.parse(url);
        String filename = new File(url).getName();
        DownloadManager downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(uri);
        request.allowScanningByMediaScanner();
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, getString(R.string.app_name) + "/" + filename);
        downloadManager.enqueue(request);
    }
}
Also used : Uri(android.net.Uri) File(java.io.File) DownloadManager(android.app.DownloadManager)

Example 53 with DownloadManager

use of android.app.DownloadManager in project apps-android-commons by commons-app.

the class MediaDetailPagerFragment method downloadMedia.

/**
     * Start the media file downloading to the local SD card/storage.
     * The file can then be opened in Gallery or other apps.
     *
     * @param m Media file to download
     */
private void downloadMedia(Media m) {
    String imageUrl = m.getImageUrl(), fileName = m.getFilename();
    // Strip 'File:' from beginning of filename, we really shouldn't store it
    fileName = fileName.replaceFirst("^File:", "");
    Uri imageUri = Uri.parse(imageUrl);
    DownloadManager.Request req = new DownloadManager.Request(imageUri);
    //These are not the image title and description fields, they are download descs for notifications
    req.setDescription(getString(R.string.app_name));
    req.setTitle(m.getDisplayTitle());
    req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
    // Modern Android updates the gallery automatically. Yay!
    req.allowScanningByMediaScanner();
    req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !(ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
        Snackbar.make(getView(), R.string.storage_permission_rationale, Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
            }
        }).show();
    } else {
        final DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(req);
    }
}
Also used : Uri(android.net.Uri) DownloadManager(android.app.DownloadManager) View(android.view.View)

Aggregations

DownloadManager (android.app.DownloadManager)53 File (java.io.File)14 Cursor (android.database.Cursor)13 Request (android.app.DownloadManager.Request)12 Uri (android.net.Uri)12 Suppress (android.test.suitebuilder.annotation.Suppress)10 Query (android.app.DownloadManager.Query)9 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Intent (android.content.Intent)5 UiObject (android.support.test.uiautomator.UiObject)5 Response (com.android.volley.Response)3 Context (android.content.Context)2 Nullable (android.support.annotation.Nullable)2 AlertDialog (android.support.v7.app.AlertDialog)2 View (android.view.View)2 Request (com.android.volley.Request)2 VolleyError (com.android.volley.VolleyError)2 FileInputStream (java.io.FileInputStream)2 JSONObject (org.json.JSONObject)2