选择文件drom dropbox选择器后,OnActivityResult()不起作用

时间:2016-01-16 19:58:55

标签: android dropbox android-download-manager

我正在尝试在Android应用中使用Dropbox Chooser Api,以允许用户从Dropbox中选择文件。我能够从片段中成功启动Dropbox Chooser,但在选择文件后,它会显示“Creating link ...”然后返回片段。我已经覆盖onActivityresult()方法来获取有关所选文件的信息。但是,遗憾的是没有调用onActivityresult()并且我无法使用下载管理器下载所选文件。

videos_fragment.java

   @Override
   public void super.onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == DBX_CHOOSER_REQUEST){
        if (resultCode == Activity.RESULT_OK){
            DbxChooser.Result result = new DbxChooser.Result(data);

            Log.i("main", "Link to selected file: " + result.getLink());

            // Handle the result
            Toast.makeText(getContext(),"Successful",Toast.LENGTH_LONG).show();

            DownloadManager.Request request = new DownloadManager.Request(result.getLink());
            request.setDescription("Downloading from App");
            request.setTitle("Downloading..");
            // in order for this if to run, you must use the android 3.2 to compile your app
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            }


     // get download service and enqueue file
            DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
        }
        else {
            Toast.makeText(getContext(),"Error connecting Dropbox",Toast.LENGTH_LONG).show();
        }
    }
    else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

打开Dropbox对话框的代码

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.videos_layout,container,false);
    mChooser = new DbxChooser("APP_KEY");
    mchooser_button= (Button)myView.findViewById(R.id.chooser_button);
    mchooser_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mChooser.forResultType(DbxChooser.ResultType.DIRECT_LINK).launch(getActivity(),DBX_CHOOSER_REQUEST);
        }
    });
    return myView;
}

2 个答案:

答案 0 :(得分:1)

您必须将<uses-permission android:name="android.permission.INTERNET"></uses-permission>添加到您的清单文件中(取自https://www.dropbox.com/developers-v1/core/sdks/android)。

如果您打算下载文件 - 您应该考虑将它们写入的位置,并根据http://developer.android.com/training/basics/data-storage/files.html添加适当的权限。

祝你好运!

答案 1 :(得分:1)

终于解决了这个!!!

我需要做的就是在mainactivity类中编写OnActivityResult方法而不是片段java。