2012-02-08 30 views
11

Tôi muốn tải một tập tin vào SDCard với lớp Android Download Manager:Cách sử dụng Android DownloadManager?

Request request = new Request(Uri.parse(url)); 
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); //set destination dir 
long downloadId = downloader.enqueue(request); 

Nhưng tôi luôn có được tình trạng download = 16 (Status_failed), và lý trí = 1008 (ERROR_CANNOT_RESUME). Tôi đã bao gồm android.permission.WRITE_EXTERNAL_STORAGE trong tệp kê khai.

Khi tôi nhận xét ra các

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename); 

và sử dụng các thư mục download mặc định, nó là OK. Nhưng tôi không biết tệp đó ở đâu, localUri tôi nhận được từ kết quả giống như sau:

content://downloads/my_downloads/95 

Tôi không biết cách sao chép tệp vào SDCard.

Điều tôi muốn tải xuống tệp là SDCard. Ai đó có thể giúp đỡ? Cảm ơn!

Trả lời

13

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() mang lại cho tôi /mnt/sdcard/downloads

Và tôi có thể sử dụng các tập tin tải về trong onReceive (ACTION_DOWNLOAD_COMPLETE)

long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); 
Query query = new Query(); 
query.setFilterById(downloadId); 
Cursor cur = dm.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)); 

     File mFile = new File(Uri.parse(uriString).getPath()); 
     .... 

    } else { 
     Toast.makeText(c, R.string.fail, Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

tôi chỉ nhận được "content: // downloads/my_downloads/539" với mã này và không có đường dẫn cục bộ. – Radon8472

+2

Tùy thuộc vào phiên bản Android, bạn có thể lấy tệp: // back (trên android <4.2) hoặc nội dung: // (trên Android 4.2 trở lên). vì vậy câu trả lời này chỉ áp dụng cho android thấp hơn 4.2 và 4.2 và lên nội dung sẽ cần phải được giải quyết với câu trả lời dưới đây bởi Min. –

15

Bạn có thể lấy đường dẫn tập tin từ localUri như thế này:

public static String getFilePathFromUri(Context c, Uri uri) { 
    String filePath = null; 
    if ("content".equals(uri.getScheme())) { 
     String[] filePathColumn = { MediaColumns.DATA }; 
     ContentResolver contentResolver = c.getContentResolver(); 

     Cursor cursor = contentResolver.query(uri, filePathColumn, null, 
       null, null); 

     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     filePath = cursor.getString(columnIndex); 
     cursor.close(); 
    } else if ("file".equals(uri.getScheme())) { 
     filePath = new File(uri.getPath()).getAbsolutePath(); 
    } 
    return filePath; 
} 
+0

Cảm ơn bạn rất nhiều, làm việc hoàn hảo với DownloadManager – xDragonZ

+0

Điều này hoạt động tốt trên 6.0.1 - kudo! – runfaj

Các vấn đề liên quan