使用Android上的Fresco库从SD卡显示图像

时间:2015-10-26 05:23:04

标签: android sd-card fresco

我可以使用Fresco图像库从URL显示图像,如其网站所示 -

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);

如何使用Fresco从SD卡显示图像?

5 个答案:

答案 0 :(得分:9)

获取设备上的文件

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

获取网络上的文件

Uri imageUri = Uri.parse(imagePath);
ImageRequest request = ImageRequest.fromUri(imageUri);
DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(draweeView.getController()).build();
draweeView.setController(controller);

答案 1 :(得分:1)

喜欢这个

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;
        }
    }
}

但是,您可能会遇到此问题

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

答案 2 :(得分:1)

如果您使用下面给出的壁画示例在RecyclerView中显示sdcard或图库中的图像。

     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);

注意file://用于显示来自SD卡的数据。

答案 3 :(得分:0)

退房:

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

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

此处在第一个链接中解释了从SD卡加载图像,如何将其与壁画连接在第二个链接中进行了解释。

答案 4 :(得分:0)

您只需使用 res:/ URI方案,然后使用资源ID ,如下所示:

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

simpleDraweeView.setImageURI(localImageUri);

以下是Fresco’s supported URIs