当用户使用android上传图片时如何从Dropbox获取共享链接?

时间:2016-05-31 16:19:25

标签: android dropbox dropbox-api

如何在用户上传图片时获取共享链接保管箱,并且我不会将链接保存到我的数据库?现在我的应用可以将图像上传到Dropbox,但无法获取链接.. 当用户点击保存时,我想将图像上传到保管箱并将链接保存到我的数据库

这是我的代码

package com.emergency.e_place;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.ProgressListener;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AppKeyPair;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Eggy on 5/3/2016.
 */
public class WC_Activity extends AppCompatActivity {
    final String TAG = "DEBUG";
    private ImageSwitcher imageview;
    private int PICK_IMAGE_REQUEST = 1;
    public static Button button1;
    public static Button button2;
    public static Button button3;
    public static Button button4;
    public static TextView textview;

    public static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Emergency_Place";
    public static File Dir = new File(path);



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wc);

        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbarWC);
        setSupportActionBar(toolbar);

        //ambil lokasi dari MainActivity
        Intent myIntent = getIntent(); // gets the previously created intent
        final String Latitude = myIntent.getStringExtra("Latitude"); // will return "FirstKeyValue"
        final String Longitude= myIntent.getStringExtra("Longitude"); // will return "SecondKeyValue"
        Log.d(TAG, "onLocationChanged: " + Longitude);
        //editTextLocation.setText(Longitude+", "+Latitude);
        //ambil lokasi dari MainActivity

        /*
        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePicture, 0);//zero can be replaced with any action code

        Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(pickPhoto , 1);//one can be replaced with any action code
        */
        //button4 = (Button) findViewById(R.id.ChooseFile);

        AndroidAuthSession session = buildSession();
        dropboxAPI = new DropboxAPI<AndroidAuthSession>(session);
        Dir.mkdirs();


        Button ChooseImage = (Button) findViewById(R.id.ChooseImage);
        ChooseImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UploadToDropboxFromSelectedApp("imageWC/"+Latitude+"_"+Longitude);
            }
        });
    }

    static DropboxAPI<AndroidAuthSession> dropboxAPI;
    private static final String APP_KEY = "idzvjf1ekzxxxx";
    private static final String APP_SECRET = "idzvjf1ekzxxxx";
    private static final String ACCESSTOKEN = "yxlTAA5L3fAAAAAAAAAAB0L-ptbcKn1Cjc5YxIpUnZcTD6uGq-kCVDJ754MY3ARt";
    private DropboxAPI.UploadRequest request;
    private AndroidAuthSession buildSession()
    {
        AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
        AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
        session.setOAuth2AccessToken(ACCESSTOKEN);
        return session;
    }
    static final int UploadFromSelectApp = 9501;
    static final int UploadFromFilemanager = 9502;
    public static String DropboxUploadPathFrom = "";
    public static String DropboxUploadName = "";
    public static String DropboxDownloadPathFrom = "";
    public static String DropboxDownloadPathTo = "";


    private void UploadToDropboxFromSelectedApp (String uploadName)
    {
        DropboxUploadName = uploadName;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(Intent.createChooser(intent, "Upload from ..."), UploadFromSelectApp);
    }

    @Override
    protected void onActivityResult(final int requestCode, int resultCode, final Intent intent) {

        Uri uri = intent.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            Log.d(TAG, "niiih : " + String.valueOf(bitmap));

            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);
            imageView.getLayoutParams().height = 300;
            imageView.getLayoutParams().width = 300;
        } catch (IOException e) {
            e.printStackTrace();
        }

        Button Simpan = (Button) findViewById(R.id.btnSimpanWC);
        Simpan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (requestCode == UploadFromSelectApp)
                {
                    Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
                    final Uri uri = intent.getData();

                    DropboxUploadPathFrom = getPath(getApplicationContext(), uri);
                    if(DropboxUploadPathFrom == null) {
                        DropboxUploadPathFrom = uri.getPath();
                    }
                    Thread th = new Thread(new Runnable(){
                        public void run() {
                            try
                            {
                                final File file = new File(DropboxUploadPathFrom);
                                InputStream inputStream = getContentResolver().openInputStream(uri);

                                dropboxAPI.putFile("/" + DropboxUploadName + file.getName().substring(file.getName().lastIndexOf("."),
                                        file.getName().length()), inputStream, file.length(), null, new ProgressListener(){
                                    @Override
                                    public long progressInterval() {return 100;}
                                    @Override
                                    public void onProgress(long arg0, long arg1){}
                                });
                                getMain().runOnUiThread(new Runnable()
                                {
                                    @Override
                                    public void run()
                                    {
                                        Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            } catch (Exception e) {e.printStackTrace();}
                        }
                    });
                    th.start();
                }
            }
        });
    }

    /*
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        switch(requestCode) {
            case 0:
                if(resultCode == RESULT_OK){
                    Uri selectedImage = imageReturnedIntent.getData();
                    imageview.setImageURI(selectedImage);
                }

                break;
            case 1:
                if(resultCode == RESULT_OK){
                    Uri selectedImage = imageReturnedIntent.getData();
                    imageview.setImageURI(selectedImage);
                }
                break;
        }
    }
    */

    public String getPath(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA };
            cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String s = cursor.getString(column_index);
            if(s!=null) {
                cursor.close();
                return s;
            }
        }
        catch(Exception e){}
        try {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            String s = cursor.getString(column_index);
            if(s!=null) {
                cursor.close();
                return s;
            }
        }
        catch(Exception e){}
        try {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            cursor.moveToFirst();
            String s = cursor.getString(column_index);
            cursor.close();
            return s;
        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public WC_Activity getMain()
    {
        return this;
    }
}

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么您正在使用v1 Android SDK(https://www.dropbox.com/developers-v1/core/sdks/android),并希望共享文件。

如果是这样,您只需要调用dropboxAPI.share并传递Dropbox中文件的路径。您将获得带有DropboxLink字段的url对象。