2

Dahili depolama çalışmalarına yükleme. Ancak, dış sdcard'a indirmeyi denediğimde, durum 2 ~ 3 dakika sonrasına kadar güncellenmez. (İmleç, .getColumnIndex (DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))) 'dan indirilen 0 bayt aldığım anlamına gelir;). Sonunda 2 ~ 3 dakika sonra indirir.DownloadManager'ı kullanarak harici depolama birimine yükleme işlemi düzgün çalışmıyor Galaxy S7

bildirim durumu da% 0

enter image description here

private void addToDownloadManager(String sourcePath, String destFolder, String deskFileName, DownloadManager downloadManager) { 
    try { 
     if(sourcePath == null || sourcePath.equals("")) 
      return; 
     try { 
      File folder = new File(destFolder); 
      if (!folder.exists()) { 
       folder.mkdirs(); 
      } 
     }catch (Exception e) { 
     } 

     Uri Download_Uri = Uri.parse(sourcePath); 
     DownloadManager.Request request = new DownloadManager.Request(Download_Uri); 

     request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); 

     request.setAllowedOverRoaming(false); 
     request.setTitle("title"); 
     request.setDescription("description"); 
     File destination = new File(destFolder, deskFileName); 
     request.setDestinationUri(Uri.fromFile(destination)); 
     downloadReference = downloadManager.enqueue(request); 
    } catch (Exception e) { 
    } 
} 

private Timer mDownloadStatusTimer; 

public void downloadStatusTimerSchedule() { 
    if (mDownloadStatusTimer != null) 
     downloadStatusTimerCancel(); 
    try { 
     mDownloadStatusTimer = new Timer("DownloadStatusTimer"); 
     DownloadStatusTimer timer = new DownloadStatusTimer(); 
     mDownloadStatusTimer.schedule(timer, 500, 500); // 0.5 second 
    } catch (Exception e) { 
    } 
} 

public void downloadStatusTimerCancel() { 
    if (mDownloadStatusTimer != null) { 
     mDownloadStatusTimer.cancel(); 
     mDownloadStatusTimer.purge(); 
     mDownloadStatusTimer = null; 
    } 
} 

private long bytes_downloaded = 0; 
private long bytes_total = 0; 

public class DownloadStatusTimer extends TimerTask { 

    @Override 
    public void run() { 
     if (mDownloadManager != null) { 
      DownloadManager.Query myDownloadQuery = new DownloadManager.Query(); 

      Cursor cursor = mDownloadManager.query(myDownloadQuery); 

      bytes_downloaded = 0; 
      bytes_total = 0; 

      try { 
       if (cursor != null && cursor.moveToFirst()) { 
        try { 
         // Get downloaded size/total size 
         if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL || 
           cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) { 
          // do nothing 
         } else { 
          bytes_downloaded += cursor.getLong(cursor 
            .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
          bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
         } 
        } catch (Exception e) { 
        } 

        while (cursor != null && cursor.moveToNext()) { 
         try { 
          // Get downloaded size/total size 
          if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL || 
            cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_FAILED) { 
           // do nothing 
          } else { 
           bytes_downloaded += cursor.getLong(cursor 
             .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); 
           bytes_total += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); 
          } 
         } catch (Exception e) { 
         } 
        } 
       } else { 
       } 
      } catch (Exception e) { 
      } finally { 
       if (cursor != null) { 
        cursor.close(); 
       } 
      } 

      Log.e("test", "Download size: " + bytes_downloaded + "/" + bytes_total); 
      MainActivity.this.runOnUiThread(new Runnable() { 
       public void run() { 
        try { 
         tv_download_status.setText("Download size: " + bytes_downloaded + "/" + bytes_total); 
        } catch (Exception e) { 
        } 
       } 
      }); 
     } 
    } 
} 

Ben Not 4 ve Galaxy S5 test edilmiş ve onlar iyi görünüyor diyor.

Android 6.0 olayı olabilir mi? veya S7? Bu bir hata mı? Yoksa burada yanlış yaptığım bir şey var mı? https://drive.google.com/open?id=0By...Ec1ZHk5ZWRkWWc

enter image description here

[Düzenle]

Hedefim: Burada https://drive.google.com/open?id=0BygTefPD845LTkp5QU1mOHRkMDQ

testine APK var:

kendiniz test etmek isterseniz, buraya kaynak koduyla proje konum: /storage/806E-1A11/Android/data/com.joshua.externalsddownloadtest/files/download/video.mp4

Kaynak: http://downloads.4ksamples.com/downloads/[2160p]%204K-HD.Club-2013-Taipei%20101%20Fireworks%20Trailer%20(4ksamples.com).mp4

bence

 File[] files = ContextCompat.getExternalFilesDirs(MainActivity.this, null); 
    destFolder = null; 
    for(int i=0; i< files.length; i++) { 
     if (!files[i].getAbsolutePath().equals(MainActivity.this.getExternalFilesDir(null).getPath())) { 
      destFolder = files[i].getAbsolutePath(); 
      break; 
     } 
    } 
    boolean bDownloadToExternal = false; 
    if(destFolder == null) { 
     tv_download_destination.setText("No external storage found"); 
    } else { 
     destFolder += "/download/"; 
     tv_download_destination.setText("Destination location: " + destFolder + fileName); 
     bDownloadToExternal = true; 
    } 
+1

okuyabilir - ne ** tam olarak ** 'hedefin' değeridir? – CommonsWare

+0

@CommonsWare, "Hedef" yolunu ekledim – jclova

+0

'DownloadManager' belki de [çıkarılabilir depolama alanı] üzerinde rasgele noktalara erişim sağlayamıyor (https://commonsware.com/blog/2014/04/09/storage-situation-removable- storage.html) Android 4.4+ üzerinde. Sıradan uygulamalar yok. – CommonsWare

cevap

1

(ı Mp4 Numune web sitesinden URL'yi var) onun Sen gibi çalışma zamanında izinleri sormalısınız android 6. yeni izin sistemi hakkında:

if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != getPackageManager().PERMISSION_GRANTED){ 
       ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, YOUR_CONST_REQUEST_CODE); 
      } 
    } 

Ve dinleyici uygulamak

@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case YOUR_CONST_REQUEST_CODE: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

// invoke your download method 

        } else { 
// if permissions was not granted 
        } 

       } 
      } 
    } 

Diğer yollarla hedefSdkVersion öğesini gradient dosyasından 23 daha az ayarlayabilirsiniz. "Dosya hedefi = new File (destFolder, deskFileName);"

Yeni izin sistemi hakkında daha fazla burada Android.com

+0

Daha önce bahsettiğim gibi, requestPermissions() & targetSdkVersion'u 23'ten daha az denedim. Sadece 2 ~ 3 dakika 'donuyor', ancak indir 'sonunda' çalışıyor. İzinle ilgisi yok. – jclova

İlgili konular