无法使用Android OneDrive Picker上传(“ NoFileSpecified”错误)

时间:2019-03-05 15:19:42

标签: android onedrive

我正在尝试使用OneDrive picker for Android中的一些示例代码。由于某些原因,我无法上载在示例中创建的文件,并且每次尝试时都会收到“ NoFileSpecified”错误。我看过其他网页也有同样的问题,但没有解决方法。

我添加了其他代码来处理API 23+权限,并向注册该应用程序的Microsoft应用程序门户添加了许多权限-但仍然无法正常工作。有什么我想念的吗?

我的代码如下:

import java.io.*;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

import com.microsoft.onedrivesdk.saver.*;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    /**
     * The default file size
     */
    private static final int DEFAULT_FILE_SIZE_KB = 100;

    /**
     * Registered Application id for OneDrive {@see http://go.microsoft.com/fwlink/p/?LinkId=193157}
     */
    private static final String ONEDRIVE_APP_ID = "4d8e8b16-70c8-4838-b879-a3b6440f2ac4";


    //region Permissions

    String[] permissions = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(this, p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100);
            return false;
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (requestCode == 100) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // do something
            }
            return;
        }
    }

    //endregion


    /**
     * The onClickListener that will start the OneDrive Picker
     */
    private final OnClickListener mStartPickingListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            final Activity activity = (Activity) v.getContext();
            activity.findViewById(R.id.result_table).setVisibility(View.INVISIBLE);

            final String filename = ((EditText)activity.findViewById(R.id.file_name_edit_text))
                    .getText().toString();
            final String fileSizeString = ((EditText)activity.findViewById(R.id.file_size_edit_text))
                    .getText().toString();
            int size;
            try {
                size = Integer.parseInt(fileSizeString);
            } catch (final NumberFormatException nfe) {
                size = DEFAULT_FILE_SIZE_KB;
            }

            // Create a file
            final File f = createExternalSdCardFile(filename, size);

            // Start the saver
            mSaver.startSaving(activity, filename, Uri.parse("file://" + f.getAbsolutePath()));
        }
    };

    /**
     * The OneDrive saver instance used by this activity
     */
    private ISaver mSaver;

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

        checkPermissions();

        // Create the picker instance
        mSaver = Saver.createSaver(ONEDRIVE_APP_ID);

        // Add the start saving listener
        findViewById(R.id.startSaverButton).setOnClickListener(mStartPickingListener);
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        // Check that we were able to save the file on OneDrive
        final TextView overallResult = (TextView) findViewById(R.id.overall_result);
        final TextView errorResult = (TextView) findViewById(R.id.error_type_result);
        final TextView debugErrorResult = (TextView) findViewById(R.id.debug_error_result);

        try {
            mSaver.handleSave(requestCode, resultCode, data);
            overallResult.setText(getString(R.string.overall_result_success));
            errorResult.setText(getString(R.string.error_message_none));
            debugErrorResult.setText(getString(R.string.error_message_none));
        } catch (final SaverException e) {
            overallResult.setText(getString(R.string.overall_result_failure));
            errorResult.setText(e.getErrorType().toString());
            debugErrorResult.setText(e.getDebugErrorInfo());
        }
        findViewById(R.id.result_table).setVisibility(View.VISIBLE);
    }

    /**
     * Creates an file on the SDCard
     * @param filename The name of the file to create
     * @param size The size in KB to make the file
     * @return The {@link File} object that was created
     */
    private File createExternalSdCardFile(final String filename, final int size) {
        final int bufferSize = 1024;
        final int alphabetRange = 'z' - 'a';
        File file = null;
        try {
            file = new File(Environment.getExternalStorageDirectory(), filename);
            final FileOutputStream fos = new FileOutputStream(file);

            // Create a 1 kb size buffer to use in writing the temp file
            byte[] buffer = new byte[bufferSize];
            for (int i = 0; i < buffer.length; i++) {
                buffer[i] = (byte)('a' + i % alphabetRange);
            }

            // Write out the file, 1 kb at a time
            for (int i = 0; i < size; i++) {
                fos.write(buffer, 0, buffer.length);
            }

            fos.close();
        } catch (final IOException e) {
            Toast.makeText(this, "Error when creating the file: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
        return file;
    }   

0 个答案:

没有答案