Android: How to upload a file to OneDrive?
-
Iam trying to upload a file using the OneDrive Saver sample app. Instead of creating a text file, I would like to select a file from the gallery and then upload it to OneDrive. I have edited the sample app but the file cannot be uploaded. I don't have any errors. Sample app available on 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); } }
-
Answer:
I think your example pretty much worked. I just cleaned it up a little and created a https://github.com/peternied/onedrive-picker-android/tree/GalleryPicker with your sample inside it. Notably, I'd expect a null ref here, which if you remove works nicely. View v = null; final Activity activity = (Activity) v.getContext(); See the full working example here: https://github.com/peternied/onedrive-picker-android/blob/GalleryPicker/SaverSample/src/com/example/onedrivesdk/saversample/SaverMain.java
user2994263 at Stack Overflow Visit the source
Related Q & A:
- How to Upload a JSP File to the smart FTP?Best solution by Yahoo! Answers
- How to upload a file to another server using cURL and PHP?Best solution by Stack Overflow
- How to cancel a file upload using an iframe?Best solution by Stack Overflow
- How to upload .jar file in Wordpress?Best solution by WordPress
- How to upload a windows movie maker file on Youtube?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.