android Dropbox身份验证和上传不同的活动

时间:2013-05-17 14:32:51

标签: java android authentication upload dropbox

我正在尝试从另一个活动中的dropbox上传文件,我将其从autenticate转移到dropbox。我有这个RegisterActivity.java用户注册,然后在注册完成后,webbrowser出现,用户必须允许Dropbox身份验证。

稍后在我的应用中,在另一项活动中,用户将视频上传到Dropbox。上传是由ASyncTask进行的,效果很好。问题是现在我不想再次进行重新认证。我该如何解决这个问题?它是在同一个会话上还是我开始新的会话?现在我试图使用山姆mDBApi形式RegisterAcitivity,但我认为这是错误的。密钥存储在storeKeys()方法中,并将它们保存在SharedPreferences中。

非常感谢您提前

这是我在pastebin中使用的RegisterActivity。 http://pastebin.com/K06JUWXv

以下是我调用ASyncTask UploadFile的活动:

public class ShowVideo extends Activity{

    final static private String ACCOUNT_PREFS_NAME = "prefs";
    final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
    final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";

    private DropboxAPI<AndroidAuthSession> mDBApi = RegisterActivity.mDBApi;
    private String[] storedKeys = getKeys();
    UploadFile upload;
    public static String path = "";
    public static String fileName;
    private VideoView ww;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //Forces landscape orientation which is what the camera uses.
        setContentView(R.layout.showvideo);
        Button yesButton = (Button) findViewById(R.id.yesButton);
        Button noButton  = (Button) findViewById(R.id.NoButton);

        Button dbButton = (Button) findViewById(R.id.dropboxButton);
        dbButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                if(v.getId() == R.id.dropboxButton){
                    if (mDBApi.getSession().isLinked() == true){
                        Log.d("ShowVideo", "TRUE");
                    }
                    if (mDBApi.getSession().isLinked() == false){
                        Log.d("ShowVideo", "FALSE");
                    }
                }
            }
        });
        yesButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(v.getId() == R.id.yesButton){
                UploadFile upload = new UploadFile(ShowVideo.this,mDBApi,path);
                upload.execute();
                }
            }
        });

        noButton.setOnClickListener(new OnClickListener() {
            public void onClick(View w) {
                        File file = new File(path);
                        boolean deleted = false;
                        deleted = file.delete();
                        Log.e("TAG", Boolean.toString(deleted));
                        Intent intent = new Intent(ShowVideo.this, CaptureVideo.class);
                        startActivity(intent);

            }
        });

        ww = (VideoView) findViewById(R.id.satisfiedVideoView);
        path = getRealPathFromURI(CaptureVideo.uriVideo);
        fileName = getFileNameFromUrl(path);

        //AndroidAuthSession session = new AndroidAuthSession(new AppKeyPair(ret[0], ret[1]), AccessType.APP_FOLDER);
        //mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    }

    private void playVideo(){
        ww.setVideoURI(CaptureVideo.uriVideo);
        ww.setMediaController(new MediaController(this));
        ww.start();
        ww.requestFocus();
    }
    public static String getFileNameFromUrl(String path) {
        String[] pathArray = path.split("/");
        return pathArray[pathArray.length - 1];
    }

    public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    /**DROPBOX-METHOD------------------------------------------*/

    private String[] getKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        String key = prefs.getString(ACCESS_KEY_NAME, null);
        String secret = prefs.getString(ACCESS_SECRET_NAME, null);
        if (key != null && secret != null) {
            String[] ret = new String[2];
            ret[0] = key;
            ret[1] = secret;
            return ret;
        } else {
            return null;
        }
    }



}

如果你看一下,这是我的ASyncTask。不应该需要。

public class UploadFile extends AsyncTask<Void, Long, Boolean> {

    DropboxAPI<AndroidAuthSession> dDBApi;
    Context dContext;
    private String SAVE_PATH;

    public UploadFile(Context context,DropboxAPI<AndroidAuthSession> mDBApi, String path) {
        dContext = context.getApplicationContext();
        dDBApi = mDBApi;
        SAVE_PATH = path;
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        FileInputStream inputStream = null;
        try {
            File file = new File(SAVE_PATH);
            inputStream = new FileInputStream(file);
            Entry newEntry = dDBApi.putFileOverwrite("/GAMES/GAME_BETWEEN_USER_A_USER_B/" + "PresentVideo.mp4", inputStream, file.length(), null); 
        } 

        catch (DropboxException e) {
            Log.e("DbExampleLog", "Something went wrong while uploading.");
        } catch (FileNotFoundException e) {
            Log.e("DbExampleLog", "File not found.");
        } catch (IOException e) {
            Log.e("DbExampleLog", "Another Exception:" + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            Log.e("DbExampleLog", "Another Exception:" + e.getMessage());
            e.printStackTrace();
        } 
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } 
                catch (IOException e) {
                }
            }
        }
        return null;
    }

}

0 个答案:

没有答案