如何访问下载文件夹中的android? [英] how to access downloads folder in android?

查看:150
本文介绍了如何访问下载文件夹中的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid,我做一个应用程序,其中一个可以(使用下载管理器)文件下载到下载文件夹中。我可以看到图片,如果我去下载文件夹中的模拟器。所以,如果我要显示的下载文件我怎样才能访问该文件夹的幻灯片?其次如何进度栏添加到此code: -

 进口java.util.Arrays中;

进口android.app.Activity;
进口android.app.DownloadManager;
进口android.app.DownloadManager.Query;
进口android.app.DownloadManager.Request;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.ImageView;

公共类Download_managerActivity延伸活动{
     专用长quueue_for_url;

     私人下载管理器DM;

        / **第一次创建活动时调用。 * /
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main);

            BroadcastReceiver的接收器=新的BroadcastReceiver(){
                @覆盖
                公共无效的onReceive(上下文的背景下,意图意图){
                    串动= intent.getAction();

                    如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){
                        长downloadId = intent.getLongExtra(
                                DownloadManager.EXTRA_DOWNLOAD_ID,0);
                        查询查询=新的Query();
                        query.setFilterById(quueue_for_url);
                        光标C = dm.query(查询);
                        如果(c.moveToFirst()){
                            INT参数:columnIndex = C
                                    .getColumnIndex(DownloadManager.COLUMN_STATUS);
                            如果(DownloadManager.STATUS_SUCCESSFUL ==Ç
                                    .getInt(参数:columnIndex)){

                                ImageView的观点=(ImageView的)findViewById(R.id.imageView1);
                                字符串uri_String_abcd = C
                                        .getString(C
                                                .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                                view.setImageURI(Uri.parse(uri_String_abcd));

                            }
                        }
                    }
                }
            };

            registerReceiver(接收器,新的IntentFilter(
                    DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }

        公共无效的onClick(视图查看){
            DM =(下载管理器)getSystemService(DOWNLOAD_SERVICE);

            请求request_for_url =新的请求(
                    Uri.parse(http://fc03.deviantart.net/fs14/i/2007/086/9/1/Steve_Jobs_portrait_by_tumb.jpg));

            请求request_for_url1 =新的请求(
                    Uri.parse("http://2.bp.blogspot.com/_q7Rxg4wqDyc/S5ZRVLxVYuI/AAAAAAAAAvU/fQAUZ2XFcp8/s400/katrina-kaif.jpg"));
            请求request_for_url2 =新的请求(
                    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);

        }

        公共无效showDownload(查看视图){
            意图I =新意图();
            //尝试更多的选择,以显示下载,检索和完整
            i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
            startActivity(ⅰ);
        }
    }
 

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

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=cmpe235.lab1
      安卓版code =1
      机器人:VERSIONNAME =1.0>
    <使用-SDK安卓的minSdkVersion =9/>

<使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>

    <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
        <活动机器人:名称=。Download_managerActivity
                  机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;

    < /用途>
< /舱单>
 

main.xml中: -

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>


    <按钮的android:文本=开始下载机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
         机器人:layout_gravity =center_vertical | center_horizo​​ntal |中心机器人:文字颜色=#0000A0
         机器人:字体=衬线安卓的onClick =的onClick>< /按钮>

    <按钮的android:文本=查看下载机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
         机器人:layout_gravity =center_vertical | center_horizo​​ntal |中心机器人:文字颜色=#0000A0
         机器人:字体=衬线安卓的onClick =showDownload>< /按钮>

    < ImageView的机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / imageView1
        机器人:SRC =@可绘制/图标机器人:layout_width =WRAP_CONTENT>< / ImageView的>

< / LinearLayout中>
 

解决方案

对于第一个问题试试 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); (自API 8) 要访问该目录中的各个文件请使用的File.List()或<一href="http://developer.android.com/reference/java/io/File.html#listFiles%28%29">File.listFiles(). 看来,报告下载进度只能在通知,请参见这里

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>

解决方案

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.

这篇关于如何访问下载文件夹中的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆