猿问

从文件路径显示图像视图?

从文件路径显示图像视图?

我需要只使用文件名来显示图像,而不是使用资源ID。

ImageView imgView = new ImageView(this);imgView.setBackgroundResource(R.drawable.img1);

我在可绘图文件夹中有图像img 1。我想从文件中显示这张图片。

我该怎么做?


慕的地6264312
浏览 426回答 3
3回答

慕姐4208626

Labeeb是正确的,如果您的资源已经放置在资源文件夹中,那么为什么需要使用PATH设置图像,只有当图像存储在SD卡中时,才需要这种路径。和尝试下面的代码来设置存储在SD卡中的文件中的位图图像。File&nbsp;imgFile&nbsp;=&nbsp;new&nbsp;&nbsp;File("/sdcard/Images/test_image.jpg");if(imgFile.exists()){ &nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;myBitmap&nbsp;=&nbsp;BitmapFactory.decodeFile(imgFile.getAbsolutePath()); &nbsp;&nbsp;&nbsp;&nbsp;ImageView&nbsp;myImage&nbsp;=&nbsp;(ImageView)&nbsp;findViewById(R.id.imageviewTest); &nbsp;&nbsp;&nbsp;&nbsp;myImage.setImageBitmap(myBitmap);}并将此权限包含在清单文件中:<uses-permission&nbsp;android:name="android.permission.WRITE_EXTERNAL_STORAGE"&nbsp;/>

沧海一幻觉

我想你可以用这个Bitmap&nbsp;bmImg&nbsp;=&nbsp;BitmapFactory.decodeFile("path&nbsp;of&nbsp;your&nbsp;img1");imageView.setImageBitmap(bmImg);

长风秋雁

您还可以使用: &nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;imgFile&nbsp;=&nbsp;new&nbsp;&nbsp;File(“filepath”); &nbsp;&nbsp;&nbsp;&nbsp;if(imgFile.exists()) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageView&nbsp;myImage&nbsp;=&nbsp;new&nbsp;ImageView(this); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myImage.setImageURI(Uri.fromFile(imgFile)); &nbsp;&nbsp;&nbsp;&nbsp;}这将对您执行位图解码。
随时随地看视频慕课网APP

相关分类

Android
我要回答