【问题标题】:Downloading PDF files in internal storage在内部存储中下载 PDF 文件
【发布时间】:2019-07-01 10:38:36
【问题描述】:

我正在做 Android 的任务。

我从 URL 下载文件并保存在 android 手机的内部存储/目录中?我的代码写在下面,但这是外部存储代码,我想要内部存储代码。

 public void onClick(View v) {
                    Toast.makeText(Computer.this,"Please Wait until the file is download",Toast.LENGTH_LONG).show();
                    downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    Uri uri = Uri.parse("url");
                    DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                    request.setAllowedOverRoaming(false);
                    request.setTitle("" + "filename" + ".pdf");
                    request.setVisibleInDownloadsUi(true);
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    Long reference = downloadManager.enqueue(request);
                    request.setDestinationInExternalFilesDir(Environment.DIRECTORY_DOWNLOADS, "/"+ "filename");
                    refid = downloadManager.enqueue(request);
                    Log.e("OUT", "" + refid);

这是外部存储代码,但我想保存在Android手机的内部存储中。

【问题讨论】:

    标签: java android android-internal-storage


    【解决方案1】:

    只有您自己的应用可以Access 到应用internal storage 默认android 内置下载管理器无法访问您的应用内部存储,因此您无法在内部存储中下载。

    解决方案:

    将sd卡中的文件下载为临时文件,下载完成后注册接收器,然后从外部存储复制删除文件后将文件从外部复制到内部存储。

    完整代码:

    public class MainActivity extends Activity {
        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_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);
                        DownloadManager.Query query = new DownloadManager.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)) {
    
                                ImageView view = (ImageView) findViewById(R.id.imageView1);
                                String uriString = c
                                        .getString(c
                                                .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
    
                                Uri a = Uri.parse(uriString);
                                File d = new File(a.getPath());
                               // copy file from external to internal storage..After that delete file from external storage..Code will easily avalible on google.
                                view.setImageURI(a);
                            }
                        }
                    }
                }
            };
    
            registerReceiver(receiver, new IntentFilter(
                    DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }
    
        public void onClick(View view) {
            dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(
                    Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png")).setDestinationInExternalPublicDir("/Sohail_Temp", "test.jpg");
            enqueue = dm.enqueue(request);
        }
    
        public void showDownload(View view) {
            Intent i = new Intent();
            i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
            startActivity(i);
        }
    }
    

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="Start Download"></Button>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="showDownload"
            android:text="View Downloads"></Button>
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image_1"></ImageView>
    </LinearLayout>
    

    权限:

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

    【讨论】:

    • 先生,如果我单击下载按钮,则文件将保存在内部存储中,我不想保存在外部存储中,请将文件从外部移动到内部目录。您给定的代码正在外部/sd 存储中下载文件,而新的 android 手机没有使用他们使用内部存储的 sd 卡。
    【解决方案2】:

    您不能使用DownloadManager 直接下载到your app's portion of internal storage。您将需要use OkHttp 或其他一些进程内 HTTP 客户端 API。

    【讨论】:

    • 我通过按钮点击方法从内部存储访问pdf文件
    【解决方案3】:

    你可以简单地删除这一行,request.setDestinationInExternalFilesDir()

    默认情况下,下载内容会保存到共享下载缓存中生成的文件名

    From Docs

    【讨论】:

    • 我试试这个代码,但文件保存在 sd 卡下载文件夹中。我想将文件保存在手机存储下载文件夹中
    【解决方案4】:

    你可以用这个:

    request.setDestinationUri(Uri.fromFile(new File(context.getCacheDir(),"filename.txt")));
    

    【讨论】:

    • 我使用此代码,但发生错误我想在内部存储文件夹中归档文件的空对象引用错误
    • 尝试 request.setDestinationUri(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + File.separator + "filename.txt")));
    • 我现在试试这个代码,但先生保存在 sd 卡中的文件我想将文件保存在手机存储中
    猜你喜欢
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2014-05-31
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多