如何在android中访问下载文件夹?

Dhr*_*ruv 71 android android-progressbar

我是新的android,我正在制作一个应用程序,其中一个可以下载文件到下载文件夹(使用下载管理器).如果我去模拟器中的下载文件夹,我可以看到图片.因此,如果我想显示下载文件的幻灯片,我如何才能访问该文件夹?其次,如何为此代码添加进度条: -

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想添加一个按钮,它执行从下载文件夹中拍摄图片的功能,然后像幻灯片一样显示.

<?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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

小智 203

第一个问题请试试

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
Run Code Online (Sandbox Code Playgroud)

(自API 8起可用)

要访问此目录中的单个文件,请使用File.list()File.listFiles().似乎只能在通知中报告下载进度,请参见此处.

  • `getExternalStoragePublicDirectory` 已弃用。请参阅[此处](https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String)) (8认同)
  • 还有一件事,当我们通过Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)访问externa_downloads时; 如何进一步访问该文件夹中的每个文件? (3认同)

小智 26

您需要在manifest.xml文件中设置此权限

android.permission.WRITE_EXTERNAL_STORAGE
Run Code Online (Sandbox Code Playgroud)

  • 从API级别23(Marshmallow)开始,您还需要在运行时请求权限.有关详细信息,请参阅[使用系统权限](http://developer.android.com/training/permissions/index.html). (4认同)

Yog*_*ity 24

更新

getExternalStoragePublicDirectory()弃用

要从Fragment

val downloadFolder = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
Run Code Online (Sandbox Code Playgroud)

Activity,

val downloadFolder = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
Run Code Online (Sandbox Code Playgroud)

downloadFolder.listFiles()将列出Files。

downloadFolder?.path 将为您提供下载文件夹的字符串路径。

更新:

在最新版本的 Android 中,对存储的直接文件系统访问受到限制。

context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)给你目录/emulated/0/Android/data/{package}/files/Download。如果您想拥有公共下载文件夹,请查看 Android 文档的存储访问框架。

  • 这实际上会在扩展文件目录下获取一个名为“Download”的文件夹,而不是公共 \Download 文件夹。有关如何获取真正的通用下载文件夹的任何更新吗? (10认同)

Mar*_* Lu 8

如果您使用的是Marshmallow,则必须:

  1. 在运行时请求权限(用户将允许或拒绝请求)或:
  2. 用户必须进入设置 - >应用程序 - > {您的应用程序} - >权限授予存储访问权限.

这是因为在Marshmallow中,Google彻底改变了权限的工作方式.


Bor*_*ris 5

如果您使用的是 shell,则下载(无“s”)文件夹的文件路径为

/storage/emulated/0/Download
Run Code Online (Sandbox Code Playgroud)