87

I am new android, i'm making an app in which one can download files to downloads folder (using Download Manager). I can see pictures if i go to downloads folder in emulator. So if i want to show a slideshow of downloaded files how can i get the access to that folder? Secondly how to add progress bar to this code:--

import java.util.Arrays;

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;

public class Download_managerActivity extends Activity {
     private long quueue_for_url;

     private DownloadManager dm;

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

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

                                ImageView view = (ImageView) findViewById(R.id.imageView1);
                                String uri_String_abcd = c
                                        .getString(c
                                                .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                                view.setImageURI(Uri.parse(uri_String_abcd));

                            }
                        }
                    }
                }
            };

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

        public void onClick(View view) {
            dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

            Request request_for_url = new Request(
                    Uri.parse("http://fc03.deviantart.net/fs14/i/2007/086/9/1/Steve_Jobs_portrait_by_tumb.jpg"));

            Request request_for_url1 = new Request(
                    Uri.parse("http://2.bp.blogspot.com/_q7Rxg4wqDyc/S5ZRVLxVYuI/AAAAAAAAAvU/fQAUZ2XFcp8/s400/katrina-kaif.jpg"));
            Request request_for_url2 = new Request(
                    Uri.parse("http://www.buzzreactor.com/sites/default/files/Bill-Gates1.jpg"));

            quueue_for_url = dm.enqueue(request_for_url);
            quueue_for_url = dm.enqueue(request_for_url1);
            quueue_for_url = dm.enqueue(request_for_url2);

        }

        public void showDownload(View view) {
            Intent i = new Intent();
            //try more options to show downloading , retrieving and complete
            i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
            startActivity(i);
        }
    }

I want to add a button which performs the function of taking the pictures from downloads folder and then display like a slideshow.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cmpe235.lab1"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Download_managerActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

main.xml:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <Button android:text="Start Download" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
         android:layout_gravity="center_vertical|center_horizontal|center" android:textColor="#0000A0" 
         android:typeface="serif" android:onClick="onClick"></Button>

    <Button android:text="View Downloads" android:id="@+id/button2"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
         android:layout_gravity="center_vertical|center_horizontal|center" android:textColor="#0000A0" 
         android:typeface="serif" android:onClick="showDownload"></Button>

    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>
Yogesh Umesh Vaity
  • 16,749
  • 10
  • 82
  • 69
Dhruv
  • 1,468
  • 7
  • 27
  • 39

6 Answers6

233

For your first question try

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 

(available since API 8)

To access individual files in this directory use either File.list() or File.listFiles(). Seems that reporting download progress is only possible in notification, see here.

Derlin
  • 8,518
  • 2
  • 22
  • 42
slkorolev
  • 5,188
  • 1
  • 30
  • 29
  • @slkorolev- hey thanks, how can i add progress bar to this code, can you please help me – Dhruv Oct 26 '11 at 21:03
  • 3
    and one more thing, that when we get access to externa_downloads by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); how to further access each file in that folder? – Dhruv Oct 26 '11 at 21:44
  • 1
    Does writing to DIRECTORY_DOWNLOADS require the permission of WRITE_EXTERNAL_STORAGE ? If so, is there an alternative? – android developer Nov 03 '15 at 06:23
  • @androiddeveloper - Yes, you need that permission. The only alternative would be to rely on some other app (like a file manager) that exposes a "save file" action you could access via an intent. There are no standard intents for doing that, however. – Ted Hopp Dec 20 '15 at 20:55
  • You can also see the answer for internal memory https://stackoverflow.com/questions/21724706/how-to-get-my-android-device-internal-download-folder-path – Majedur Rahaman Jan 20 '19 at 10:16
  • 6
    `getExternalStoragePublicDirectory` is deprecated. Refer [here](https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String)) – Ashish Emmanuel Apr 18 '20 at 03:57
  • @AshishEmmanuel you post it is deprecated, with a link to prove it, but how do you do it with MediaStore? – bradrice May 06 '21 at 22:45
27

You need to set this permission in your manifest.xml file

android.permission.WRITE_EXTERNAL_STORAGE
Bo Persson
  • 86,087
  • 31
  • 138
  • 198
mac
  • 271
  • 3
  • 2
  • 7
    As of API level 23 (Marshmallow) you also need to request the permission at run time. See [Working with System Permissions](http://developer.android.com/training/permissions/index.html) for more info. – Ted Hopp Dec 20 '15 at 20:05
  • Also starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by Context.getExternalFilesDir(String) and Context.getExternalCacheDir(). https://developer.android.com/reference/android/Manifest.permission#READ_EXTERNAL_STORAGE , https://developer.android.com/reference/android/Manifest.permission#WRITE_EXTERNAL_STORAGE. But I faced java.io.FileNotFoundException (Permission denied) on attempt FileOutputStream(new File(getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS), lfile)) – Andrew Glukhoff Apr 20 '20 at 08:45
24

Updated

getExternalStoragePublicDirectory() is deprecated.

To get the download folder from a Fragment,

val downloadFolder = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)

From an Activity,

val downloadFolder = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)

downloadFolder.listFiles() will list the Files.

downloadFolder?.path will give you the String path of the download folder.

Update:

The Direct filesystem access to storage has become limited in recent versions of Android.

context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) gives you the directory /emulated/0/Android/data/{package}/files/Download. If you want to have the public downloads folder, look into the Storage Access Framework of Android documentation.

Yogesh Umesh Vaity
  • 16,749
  • 10
  • 82
  • 69
11

If you are using Marshmallow, you have to either:

  1. Request permissions at runtime (the user will get to allow or deny the request) or:
  2. The user must go into Settings -> Apps -> {Your App} -> Permissions and grant storage access.

This is because in Marshmallow, Google completely revamped how permissions work.

Juri Noga
  • 4,205
  • 7
  • 32
  • 45
Mark Lu
  • 1,188
  • 1
  • 11
  • 18
3

If you're using a shell, the filepath to the Download (no "s") folder is

/storage/emulated/0/Download
Boris
  • 7,044
  • 6
  • 62
  • 63
2

You should add next permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And then here is usages in code:

val externalFilesDir = context.getExternalFilesDir(DIRECTORY_DOWNLOADS)
Yvgen
  • 1,461
  • 13
  • 24
  • 1
    getExternalFilesDir() gives you the private directories for the app. And for that reason, you don't need any permission since Android 4.4 – Simon Apr 19 '21 at 02:15