Android:如何将文件上传到OneDrive?

时间:2015-04-18 21:53:47

标签: android file upload onedrive

我尝试使用OneDrive Saver示例应用上传文件。我想从图库中选择一个文件,然后将其上传到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);
    }


}

1 个答案:

答案 0 :(得分:1)

我认为你的例子非常有效。我刚清理了一下,然后在里面创建了一个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