How to upload image files from gallery using retrofit 2? - android

I am a little new to android and have been trying to send data to server using retrofit 2.What i am doing is selecting images from gallery and getting their paths but i have a less knowledge of how uploading works.
I have multiple images uri from gallery and using this function i get their paths
public String getImagePath(Uri uri) {
String selectedImagePath;
// 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
selectedImagePath = cursor.getString(column_index);
} else {
selectedImagePath = null;
}
if (selectedImagePath == null) {
selectedImagePath = uri.getPath();
}
return selectedImagePath;
}
this gives me the paths like this.
for example: storage/emulated/0/Download/cheque_image.jpeg
do i need only these paths to create files
File file = new File(getimagepath(uri));
RequestBody requestBody = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);
Does the file created from path is enough to be uploaded using multipart or should inputstream from uri is used to be passed inside file and then to upload.
OutputStream outputStream = new FileOutputStream(file);

Related

Cursor returns null when getting filepath from URI

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
cursor.getString(columnIndex) Returns Null
Where as the selectedImage is not null.
Any suggestion on how I can create file from URI?
Cursor returns null when getting filepath from URI
A Uri is not a file. A Uri does not have to point to a file, let alone a file on the filesystem that you can access.
Any suggestion on how I can create file from URI?
Use ContentResolver and openInputStream() to get an InputStream on the content identified the Uri. Ideally, just use that InputStream directly. If, for some reason, you truly need a file:
Create a FileOutputStream on some file that you control (e.g., in getCacheDir())
Copy the bytes from the InputStream to the FileOutputStream
Use the file that you just created, deleting it when you are done
Use this method to get path:
public String getPath(Uri uri) {
// just some safety built in
if (uri == null) {
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
And in onACtivityResult use this:
Uri selectedImage = data.getData();

Find out the camera picture location

Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"
I tried this code for find the location of camera storage images but its give only "DCIM" image location.but some devices not store always in "DCIM" folder so how could i find out storage location?
Try this:
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Try to below code work for my app perfect :-
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
Method:-
public String getRealPathFromURI(Uri contentUri)
{
try
{
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);
}
catch (Exception e)
{
return contentUri.getPath();
}
}

cant get file path from URI

I am trying to open a intent that lets me choose a file. Im able to select a file but when I try creating a file with the Uri I got in the OnActivityResult method I get a file size of 0. I dont think Im getting the right file path.
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/TESTAPP4");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri data = Uri.fromFile(file);
String type = "*/*";
intent.setDataAndType(data, type);
startActivityForResult(intent, 12);
onActivityResult:
Uri u= data.getData();
File file = new File( u.getpath);
file.length() // give 0
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Get filename and path from URI from mediastore

Android how to get the only uri of Media Store Image?

I'm trying to get the uri and after parse into path of MediaStore image.
I do this to get the uri:
Uri img_uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
And after with this function i convert uri in the path:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
But the result is that i have the path of the Media storage but with the first element.
For example: /mnt/sdcard/Pictures/Boat.jpg. But i want only /mnt/sdcard/Pictures/.
Please don't tell me to use join and split because isn't what i want.
Use this:
String path = getExternalFilesDir(null).getAbsolutePath();
to get the main directory in your app subfolder where you can put another directory, for example "Images" in this way:
String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
File folder = new File(path);
if (!folder.exists()) {
folder.mkdir();
}
So now with this:
String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
you can access to this folder without problems.

How to get the path of the image downloaded from third party application and stored in sd card

I am trying to open the gallery and select the image from there.I got the path of all the image which are captured from camera but cannot get the real path of the image which were downloaded from Facebook/picassa etc.The path it is giving is like https://lh3.googleusercontent.com/XNzSBp0MycQ/TigFxMIWn2I/AAAAAAAAAAg/YJPWAWmGOy0/I/11%252520-%2525201.jpg even though it is in gallery.
Here is my code ::
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
i am using the following code to get the path
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
public String getPath(Uri uri) {
int columnIndex = 0;
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String imagePath = cursor.getString(columnIndex);
return imagePath;
} else {
return null;
}
String filename = "image.jpg";
String path = "/mnt/sdcard/" + filename;
File f = new File(path); //
Uri imageUri = Uri.fromFile(f);

Categories

Resources