简体   繁体   English

如何使用Android下载管理器将下载的图像存储在内部存储中

[英]how to store downloaded image in internal storage using Download Manager in android

how to save image or mp3 file in internal storage using Download manager 如何使用下载管理器在内部存储中保存图像或mp3文件

Code: 码:

   public void StartDownload(String path)
   {
   ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("channel" + cId, Context.MODE_PRIVATE);
        if (!directory.exists()) {
            directory.mkdir();
        }
        DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

        Uri downloadUri = Uri.parse(path);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri);
        String imgnm = path.substring(path.lastIndexOf("/") + 1);
        startdownloadurl = directory.getAbsolutePath()+"/";
        System.out.println(" directory " + startdownloadurl);
        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle(imgnm)
                .setDescription("Downloading...")
                .setVisibleInDownloadsUi(true)
                .setDestinationInExternalPublicDir(startdownloadurl, imgnm);


        mgr.enqueue(request);
    }

im trying to store downloaded image or mp3 file in my internal storage but it not work well required path is "data/data/packagename/app_channel1/image1.jpg" 我试图将下载的图像或MP3文件存储在我的内部存储器中,但它不能正常工作所需的路径是“data / data / packagename / app_channel1 / image1.jpg”

Only your own app can Access to app internal storage Default android builtin download manager cant access to your app internal storage so you cant download in internal storage. 只有您自己的应用程序才能Access应用程序internal storage默认的android内置下载管理器无法访问您的应用程序内部存储,因此您无法下载内部存储。

Solution: 解:

Download file in sd card temp file and when download complete register a receive and then copy file from external to internal storage. sd card临时文件中下载文件,并在下载完成时register a receive ,然后将文件从external internal storage.复制到internal storage.

Complete code: 完整代码:

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 will esaily avalible on net use 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);
    }
}

Layout: 布局:

<?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>

Permissions: 权限:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android的外部存储中使用Downloader Manager下载的图像名称存储图像 - How to store image using Downloader Manager downloaded image name in external storage in android 使用下载管理器将媒体存储在内部存储中 - Store media in internal storage using download manager 如何在Android中使用JSON从服务器下载图像并将其存储在内部存储中 - how to download image from server and store in internal storage using JSON in android Android,如何在内部存储中存储图像? - Android, How to store image in internal storage? 将图像存储在android的内部存储中 - store an image in internal storage in android 如何在内部存储中下载和存储android中的文件并访问它? - How to download & store files in android in internal storage and access it? 打开使用Android下载管理器下载的文件 - Open a file downloaded using download manager Android 如何将下载的文件保存到android的内部存储中? - how to save a downloaded file into internal storage in android? Android - 存储下载内容的位置,内部存储与外部存储? - Android – Where to store downloaded content, internal versus external storage? Android-如何存储和从内部存储检索图像 - Android - How to store and retrieve the image from internal storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM