Android: 如何上传文件到OneDrive?

Android: How to upload a file to OneDrive?

我正在尝试使用 OneDrive Saver 示例应用上传文件。我不想创建文本文件,而是想从库中 select 一个文件,然后将其上传到 OneDrive。我已经编辑了示例应用程序,但无法上传文件。我没有任何错误。

示例应用可在 GitHub (https://github.com/OneDrive/onedrive-picker-android/tree/master/SaverSample) 上使用。

package com.example.onedrivesdk.saversample;

import java.io.*;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.*;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import com.microsoft.onedrivesdk.saver.*;

public class SaverMain extends Activity {

    private static final int DEFAULT_FILE_SIZE_KB = 100;
    static final int                RESULT_STORE_FILE = 4;
    private static Uri              mFileUri;
    private static final String ONEDRIVE_APP_ID = "4813EF88";


    private final OnClickListener mStartPickingListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {

            final Intent galleryIntent = new Intent(Intent.ACTION_PICK);
            galleryIntent.setType("*/*");
            startActivityForResult(galleryIntent, RESULT_STORE_FILE);

        }
    };


    private ISaver mSaver;

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

        // 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) {

        try {
            mSaver.handleSave(requestCode, resultCode, data);

        } catch (final SaverException e) {

        }
        switch (requestCode) 
        {

            case RESULT_STORE_FILE:

                mFileUri = data.getData();
                saveFileToDrive();
                break;

        }


    }


    private void saveFileToDrive() 
    {
        Thread t = new Thread(new Runnable() 
        {
            @Override
            public void run() 
            {
                try 
                {
                    // Create URI from real path
                    String path;
                    path = getPathFromUri(mFileUri);
                    mFileUri = Uri.fromFile(new java.io.File(path));

                    ContentResolver cR = SaverMain.this.getContentResolver();

                    // File's binary content
                    java.io.File fileContent = new java.io.File(mFileUri.getPath());
                    View v = null;
                    final Activity activity = (Activity) v.getContext();
                    mSaver.startSaving(activity, path, Uri.parse(mFileUri.toString()));


                } catch (Exception e) {

                } 
            }
        });
        t.start();
    }

    public String getPathFromUri(Uri uri) 
    {
          String[] projection = { MediaStore.Images.Media.DATA };
          Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
          int column_index = cursor
                  .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
          cursor.moveToFirst();
          return cursor.getString(column_index);
    }


}

我认为您的示例非常有效。我只是稍微清理了一下,然后创建了一个 branch,里面有你的样本。值得注意的是,我希望这里有一个空引用,如果你删除它,它会很好地工作。

View v = null;
final Activity activity = (Activity) v.getContext();

在此处查看完整的工作示例:https://github.com/peternied/onedrive-picker-android/blob/GalleryPicker/SaverSample/src/com/example/onedrivesdk/saversample/SaverMain.java