3

I can show Images from URL using Fresco image library as shown in their website -

Uri u = Uri.parse("https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png");
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(u);

How to display images from SD Card using Fresco?

Shishram
  • 1,514
  • 11
  • 20
Gissipi_453
  • 1,250
  • 1
  • 25
  • 61

5 Answers5

9

Getting files that are on device

SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
Uri imageUri= Uri.fromFile(new File(imagePath));// For files on device
draweeView.setImageURI(imageUri);

Getting files that are on the network

Uri imageUri = Uri.parse(imagePath);
ImageRequest request = ImageRequest.fromUri(imageUri);
DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(draweeView.getController()).build();
draweeView.setController(controller);
Eftekhari
  • 1,001
  • 1
  • 19
  • 37
  • I there a way to add fileUri and networkurl in the XML itself? @eftekhari – Vikas Pandey Feb 16 '22 at 14:15
  • @VikasPandey Maybe saving in `SharedPreferences` would be a way to go. https://stackoverflow.com/questions/7169485/how-to-use-sharedpreferences-to-save-a-uri-or-any-storage – Eftekhari Feb 16 '22 at 17:17
2

Like this

SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);

draweeView.setImageURI(getImageContentUri(context.filepath));

/**
 * Gets the content:// URI  from the given corresponding path to a file
 *
 * @param context
 * @param imageFile
 * @return content Uri
 */
public static Uri getImageContentUri(Context context, java.io.File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Images.Media._ID},
            MediaStore.Images.Media.DATA + "=? ",
            new String[]{filePath}, null);
    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/images/media");
        return Uri.withAppendedPath(baseUri, "" + id);
    } else {
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}

However, you may encounter this problem

How i can update image of fresco SimpleDraweeView, if it was set once by setImageURI

Community
  • 1
  • 1
hedgehog
  • 637
  • 7
  • 8
1

Check out:

https://dzone.com/articles/displaying-images-sd-card

http://frescolib.org/docs/supported-uris.html#_

Here loading images from sd card is explained in first link and how to connect it with fresco is explained in second link.

1

If you are showing images from sdcard or gallery in RecyclerView using fresco example given below.

     Uri imageUri = Uri.parse("file://"+galleryList.get(position));

    ImageRequest request = ImageRequest.fromUri(imageUri);

    DraweeController controller = Fresco.newDraweeControllerBuilder()
            .setImageRequest(request)
            .setOldController(holder.simpleDraweeViewOne.getController()).build();
    Log.e(TAG, "ImagePath uri " + imageUri);

    holder.simpleDraweeViewOne.setController(controller);

Note: file:// used for showing data from sdcard.

Rick
  • 1,177
  • 1
  • 19
  • 27
iamkdblue
  • 3,448
  • 2
  • 25
  • 43
0

You just need to use the res:/ URI scheme followed by the ID of your resource, like this:

Uri localImageUri = Uri.parse("res:/" + R.drawable.your_sdcard_image);

simpleDraweeView.setImageURI(localImageUri);

Here are Fresco’s supported URIs.