Search code examples
androidandroid-download-manager

How to download file in my SD card folder in Android


I want download file from server and for this work i use this library : https://github.com/ayz4sci/DownloadProgress , and write below codes :

public class MainActivity extends AppCompatActivity implements DownloadProgressView.DownloadStatusListener {

    private Button downloadButton, playButton;
    private long downloadID;
    private String downloadURL = "http://cld8.cdn.p30download.com/p30dl-mobile/AppMgr.Pro.III.v4.00.Patched_p30download.com.apk";
    private DownloadManager downloadManager;
    private DownloadProgressView downloadProgressView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        playButton = (Button) findViewById(R.id.playButton);

        final File myDirectory = new File(Environment.getExternalStorageDirectory(), "dirName");

        if(!myDirectory.exists()) {
            myDirectory.mkdirs();
        }

        downloadProgressView = (DownloadProgressView) findViewById(R.id.downloadProgressView);
        downloadButton = (Button) findViewById(R.id.downloadButton);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                request.setTitle("You garrit");
                request.setDescription("DownloadProgress sample");
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalFilesDir(getApplicationContext(), "myCustomFolderName", "p30download.com.apk");
                request.allowScanningByMediaScanner();

                downloadID = downloadManager.enqueue(request);
                downloadProgressView.show(downloadID, new DownloadProgressView.DownloadStatusListener() {
                    @Override
                    public void downloadFailed(int reason) {

                    }

                    @Override
                    public void downloadSuccessful() {

                        playButton.setVisibility(View.VISIBLE);
                        playButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.fromFile(getFileUri()), "apk/*");
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void downloadCancelled() {
                        downloadButton.setVisibility(View.VISIBLE);
                    }
                });
                downloadButton.setVisibility(View.GONE);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public File getFileUri() {
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(downloadID);
        Cursor cur = downloadManager.query(query);

        if (cur.moveToFirst()) {
            int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
            if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) {
                String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                return new File(Uri.parse(uriString).getPath());
            }
        }

        return null;
    }

    @Override
    public void downloadFailed(int reason) {
        System.err.println("Failed :" + reason);
        downloadButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void downloadSuccessful() {
        playButton.setVisibility(View.VISIBLE);
    }

    @Override
    public void downloadCancelled() {
        downloadButton.setVisibility(View.VISIBLE);
    }
}

I want when download successful, save this file into my folder. for this work i use this code :

request.setDestinationInExternalFilesDir(getApplicationContext(), "myCustomFolderName", "p30download.com.apk");

but when download finished i can't show myCustomFolder name in SD card, and i don't know where save my downloaded file!

How can i fix this issue? Please help me, thanks all <3


Solution

  • use below code inside onCLick of download

      File sdCard = Environment.getExternalStorageDirectory();
                    File dir = new File (sdCard.getAbsolutePath() + "/myCustomFolderName");
                    dir.mkdirs();
                    File file = new File(dir, "you_garrit.mp4");
                    Uri uri = Uri.fromFile(file);
    
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadURL));
                    request.setTitle("You garrit");
                    request.setDescription("DownloadProgress sample");
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationUri(uri);
                    request.allowScanningByMediaScanner();
    
                    downloadID = downloadManager.enqueue(request);
                    downloadProgressView.show(downloadID, new DownloadProgressView.DownloadStatusListener() {
                        @Override
                        public void downloadFailed(int reason) {
                            Log.d(TAG, "downloadFailed" + reason);
                        }
    
                        @Override
                        public void downloadSuccessful() {
                            Log.d(TAG, "downloadSuccessful");
                        }
    
                        @Override
                        public void downloadCancelled() {
                          Log.d(TAG, "downloadCancelled");
                        }
                    });
                    downloadButton.setVisibility(View.GONE);
    

    please let me know if these dosen't work for you