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) {
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) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
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];
}
}
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);
}
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);
}
}
else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
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();
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();
}
}