1

how to get shared link dropbox when user upload image and i wan't save the link to my database?? now my apps can upload the image to dropbox but can't get the link.. when user click save i wan't the image upload to dropbox and the link save to my database

this is my code

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 Answer 1

1

If I understand your question correctly, you're using the v1 Android SDK (https://www.dropbox.com/developers-v1/core/sdks/android) and you want to share a file.

If so, you just need to call dropboxAPI.share and pass in the path to the file in Dropbox. You'll get back a DropboxLink object with a url field.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.