Click here to Skip to main content
15,902,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I able to use the following code to get image from Dropbox, it opens up Droxbox and is able to select and download the image. However, the issue is that the downloaded image is not able to be show on the ImageView. Please help, Thank you.

If my method does not work, please suggest other ways that i can used to get image from dropbox to the imageview. Thank You.

What I have tried:

Code: getImage

Image.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                List<String> data= new ArrayList<String>();
                data.add("Import from local device.");
                data.add("Take camera image.");

                AdapterGetImage adapter = new AdapterGetImage(CropImageActivity.this);

                adapter.setData(data);

                new AlertDialog.Builder(CropImageActivity.this).setAdapter(adapter, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if (which==0) {
                            final Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent, "Select File"),GALLERY_CHOOSER_INTENT);
                        }
                        else if(which==1)
                        {

                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            if (intent.resolveActivity(getPackageManager()) !=null)
                            {
                                File photoFile = null;
                                try{
                                    photoFile = createImageFile();
                                }
                                catch (IOException ex)
                                {
                                    ex.printStackTrace();
                                }
                                if (photoFile !=null)
                                {
                                    Uri photoURI = FileProvider.getUriForFile(CropImageActivity.this,
                                            "fileprovider",photoFile);
                                    intent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                                    startActivityForResult(intent,CAMERA_IMAGE);
                                }
                            }

                        }

                    }

                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setCancelable(false)
                .show();

                });


Code: get path of image
public static String getPath(final Context context, final Uri uri) {

        // check here to KITKAT or new version
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/"
                            + split[1];
                }
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"),
                        Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };

                return getDataColumn(context, contentUri, selection,
                        selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {

            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();

            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }


Code: Activity result
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {


            if (requestCode == GALLERY_CHOOSER_INTENT) {
                if (null == data) return;

                String selectedImagePath;
                Uri selectedImageUri = data.getData();

                //MEDIA GALLERY
                selectedImagePath = getPath(getApplicationContext(), selectedImageUri);


                cropImageView.requestLayout();
                LoadPicture(selectedImagePath);
                Toast.makeText(this,"uri" +selectedImagePath,Toast.LENGTH_LONG).show();

            }


        }
        catch (Throwable ex) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ImageActivity.this);
            builder.setTitle("Error:");
            builder.setMessage("We apologize for the inconvenience caused, data" +data.getData()
                    +"\n"
                    +"\n"
                    +ex.toString())
                    .setCancelable(false)
                    .setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    Intent goToStart = new Intent(
                                            CropImageActivity.this,
                                            HomeActivity.class);
                                    goToStart
                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(goToStart);
                                }
                            });

            AlertDialog alert = builder.create();
            alert.show();

        }
    }
Posted
Comments
David Crow 3-Oct-18 9:10am    
An image is an image, regardless from whence it came. If you know the image was successfully downloaded, or if your "show image" code can show other arbitrary images that exist on your device, Dropbox can be removed from the equation. Do what you can to eliminate irrelevant things, and focus on what you are left with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900