开发者

Android ImageView set to local URL not working?

开发者 https://www.devze.com 2023-02-13 04:28 出处:网络
So I am new to Android, but I do not understand why this does not work (and how to get it to work): ImageView i = (ImageView) findViewById(R.id.image_to_display);

So I am new to Android, but I do not understand why this does not work (and how to get it to work):

ImageView i = (ImageView) findViewById(R.id.image_to_display);
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
i.setImageURI(uri)

I did a Toast and have made sur开发者_如何转开发e that uri.toString() returns a url like content://... I am also sure that i is a valid reference, because I am successfully able to set it to local images that are part of the .apk.

So why would this not work, and how can I get it to work?

Thanks


If you don't HAVE to have it be an Uri you could do something like this instead.

String fullpath = Environment.getExternalStorageDirectory() + "/pathtoyourfile"

        // take the path create a bitmap and populate the ImageView with it.
        ImageView iv = (ImageView) v.findViewById(R.id.thumbnail);
        Bitmap bm = BitmapFactory.decodeFile(fullpath);
        iv.setImageBitmap(bm);


Try loading it yourself and then passing it in:

ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);

if (bitmap != null) {
    i.setImageBitmap(bitmap);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消