当前位置: 首页 > 知识库问答 >
问题:

下载内部存储的PDF文件

万俟宜修
2023-03-14
 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手机的内部存储。

共有1个答案

甄煜
2023-03-14

只有您自己的应用程序可以访问应用程序内部存储默认的android builtin下载管理器不能访问您的应用程序内部存储,所以您不能在内部存储中下载。

解决方案

下载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" />
 类似资料:
  • 我正在完成Android的任务。 我从URL下载文件并保存在android手机的内部存储/目录中?我的代码写在下面,但这是外部存储代码,我想要内部存储代码。 这是外部存储代码,但我想保存在Android手机的内部存储中。

  • 在Android中使用和iOS中使用下载存储文件,是否仍然在暗中使用下载uri方法,即在uri末尾使用不可猜测的标记? 我的要求禁止我们向世界公开这样的URL,所以问题是: null

  • 在我的应用程序中,有一个由sqlite创建的数据库。我想将数据库复制到下载文件夹。我使用的是不同的方式,但并没有在真实的设备上工作(它说拒绝访问目录),可能是它想要根手机。请帮助,我想提供一个访问权限,以便用户可以从下载文件夹获取数据库。 请注意,我使用的是最新版本的android studio。 在下面给定的DatabaseHelper类中。在exportDB()方法中,我要实现这一点。

  • 我可以通过以下服务下载pdf文件: 但是,当我运行该服务时,我得到了以下响应: %���� 10 obj 要打开pdf,我需要用第二个鼠标按钮点击响应链接: WS网址 然后选择“在新选项卡中打开”以打开pdf文件,看起来您正在运行该服务两次以获得一次pdf文件。 我想自动打开每个请求到WS的pdf文件。 这意味着每次您请求WS时,都应该返回直接在屏幕中打开的pdf文件。 有人能帮我修一下吗? Tk

  • 我试图使用Adobe reader读取从服务器下载的pdf文件,但问题是当我将其存储在内部存储时,其他应用程序无法读取该文件。现在我想知道如何将此文件复制到外部存储(/sdcard/)中,以便pdf查看器可以查看。 由于安全原因,我将文件存储在内部存储,然后删除外部存储的文件。 我的问题是如何复制保存在内部存储中的文件,而不使用raw或将其放入InputStream中的资产。

  • 我正在开发一个应用程序,在第一次运行时,必须通过应用编程接口从网络下载文件(图像jpg/png),然后将其存储在本地,这样在线连接就不再必要了(用户可以在线运行更新,如果有的话,可以通过应用编程接口下载更新的数据将是任何可用的更新)。 我知道桌面应用程序的工作方式非常不常见,但主要目标是将桌面应用程序数据与网络应用程序同步。 到目前为止,我已经找到了一个npm插件(link),用于检查用户是否连接