无法开始使用Fetch库下载文件

时间:2019-02-26 18:04:02

标签: android

我正在使用Fetch库敌人从互联网下载文件,我使用下面的以下简化代码来开始下载无法正常工作,也无法开始下载,我在github上测试了示例代码,但我我不确定我的代码有什么问题

我检查链接网址以下载,存储许可,存储空间,Internet连接,Internet许可和访问许可,所有这些都正确无误

我没有任何错误可以知道要解决的问题,而我在logcat上仅得到以下输出:

/xx.xxxx.xxxxxxE/FILE:: http://www.diabetist.ir/saffat.mp3
/xx.xxxx.xxxxxxEE/request:: Request(url='http://www.diabetist.ir/saffat.mp3', file='/storage/emulated/0/ShahKelid/media/saffat.mp3', id=1189512191, groupId=0, headers={}, priority=HIGH, networkType=ALL, tag=null)

应用程序类:

public void onCreate() {
    super.onCreate();

    ...
    final FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this)
            .enableRetryOnNetworkGain(true)
            .setDownloadConcurrentLimit(1)
            .setHttpDownloader(new HttpUrlConnectionDownloader(Downloader.FileDownloaderType.PARALLEL))
            .build();
    Fetch.Impl.setDefaultInstanceConfiguration(fetchConfiguration);
}

我的活动课:

public class ActivityMain extends AppCompatActivity implements
        OnSheetDismissedListener,
        BottomSheetLayout.OnSheetStateChangeListener,
        SlidingLayer.OnInteractListener,
        FetchListener {

    private static final int STORAGE_PERMISSION_CODE = 100;
    @Inject
    Handler handler;

    ...

    private Fetch fetch;
    private Request request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        setContentView(R.layout.salam);

        ButterKnife.bind(this);

        activity = ActivityMain.this;
        context = getBaseContext();

        ...

        fetch = Fetch.Impl.getDefaultInstance();

        FragmentSectionsList.setPlayListenerFor(lessonId -> {
            sectionLesson = realm.where(SectionLesson.class).equalTo("id", lessonId).findFirst();
            if (sectionLesson != null) {
                if (sectionLesson.isCourse()) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                    }
                } else {
                    startActivity(new Intent(activity, ActivityLogin.class));
                }
            }
        });
    }

    private void enqueueDownload() {
        String url = sectionLesson.getFile_url();
        String file = CoreApplication.MEDIAFOLDER + "/" + sectionLesson.getFilename();

        Log.e("FILE: ", url);

        request = new Request(url, file);
        request.setPriority(Priority.HIGH);
        request.setNetworkType(NetworkType.ALL);

        fetch.enqueue(request, updatedRequest -> {
            request = updatedRequest;
            Log.e("request: ", request.toString());
        }, error -> Toast.makeText(activity, error.toString(), Toast.LENGTH_LONG).show());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == STORAGE_PERMISSION_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            enqueueDownload();
        } else {
            Snackbar.make(root_coordinator_layout, R.string.permission_not_enabled, Snackbar.LENGTH_LONG).show();
        }
    }

    private void updateViews(@NotNull Download download, long etaInMillis, long downloadedBytesPerSecond, @Nullable Error error) {
        if (request.getId() == download.getId()) {
            Timber.e("getETAString: %1$s", AndroidUtilities.getETAString(this, etaInMillis));
            Timber.e("downloadSpeedTextView: %1$s", AndroidUtilities.getDownloadSpeedString(this, downloadedBytesPerSecond));
            if (error != null) {
                showDownloadErrorSnackBar(download.getError());
            }
        }
    }

    private void showDownloadErrorSnackBar(@NotNull Error error) {
        final Snackbar snackbar = Snackbar.make(root_coordinator_layout, "دانلود فایل انجام نشد", Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction(R.string.retry, v -> {
            fetch.retry(request.getId());
            snackbar.dismiss();
        });
        snackbar.show();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

   @Override
    public void onAdded(@NotNull Download download) {
        Log.e("onAdded: ", "");
    }

    @Override
    public void onCancelled(@NotNull Download download) {
    }

    @Override
    public void onCompleted(@NotNull Download download) {
        Log.e("onCompleted: ", "");
    }

    @Override
    public void onDeleted(@NotNull Download download) {
    }

    @Override
    public void onDownloadBlockUpdated(@NotNull Download download, @NotNull DownloadBlock downloadBlock, int i) {
    }

    @Override
    public void onPaused(@NotNull Download download) {
    }

    @Override
    public void onProgress(@NotNull Download download, long etaInMilliseconds, long downloadedBytesPerSecond) {
        Log.e("onProgress: ", "");
        updateViews(download, etaInMilliseconds, downloadedBytesPerSecond, null);
    }

    @Override
    public void onQueued(@NotNull Download download, boolean b) {
        Log.e("onQueued: ", "");
    }

    @Override
    public void onRemoved(@NotNull Download download) {
        Log.e("onRemoved: ", "");
    }

    @Override
    public void onResumed(@NotNull Download download) {
        Log.e("request: ", request.toString());
    }

    @Override
    public void onStarted(@NotNull Download download, @NotNull List<? extends DownloadBlock> list, int i) {
        Log.e("onStarted: ", "");
    }

    @Override
    public void onWaitingNetwork(@NotNull Download download) {
    }

    @Override
    public void onError(@NotNull Download download, @NotNull Error error, @Nullable Throwable throwable) {
        Log.e("onError: ", throwable.getMessage());
    }

    public interface OnCloseDrawerLayout {
        void onClose();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        fetch.addListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        fetch.removeListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        fetch.close();
    }
}

0 个答案:

没有答案