ShahabGT / InstagramPicker

a simple image picker android library that looks like Instagram
42 stars 11 forks source link

Return URI or File? #2

Closed UrbanSide closed 4 years ago

UrbanSide commented 4 years ago

Is it possible to somehow make the file or uri return? I think it would be much more convenient for work, for example, with sending photos to the server or to the glide. I still was not able to understand how to change it. My method:

public void getImageFilePath(Uri uri) {
        File file = new File(uri.getPath());
        String[] filePath = file.getPath().split(":");
        String image_id = filePath[filePath.length - 1];
        Cursor cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{image_id}, null);
        if (cursor!=null) {
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            Log.e("FilePath","  "+imagePath);
            files.add(imagePath);
            cursor.close();
        }
    }

Return null on String imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); Call: getImageFilePath()

for(String url : addresses) {
                    Uri imageUri = Uri.parse(url);
                      getImageFilePath(imageUri)
}
UrbanSide commented 4 years ago

Sorry. Thiw method work

public String getPathFromUri(Uri uri, Activity activity) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
ShahabGT commented 4 years ago

you can simply use -> Uri.parse(address); and to file -> new File(Uri.parse(address));