我将项目相关图像存储在drawable文件夹中.另外,我将图像名称存储在字符串变量中,并且我正在尝试将这些图像设置为imageview.但图像没有显示.请帮助我这方面.
我的代码:
int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,"imagename"是包含图像名称的字符串变量.
提前致谢
rfs*_*raz 113
试试这个:
String uri = "@drawable/myresource"; // where myresource (without the extension) is the file
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
Run Code Online (Sandbox Code Playgroud)
Ram*_*ran 35
在这里,我将frnd_inactive
图像设置drawable
为图像
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageDrawable(getResources().getDrawable(R.drawable.frnd_inactive));
Run Code Online (Sandbox Code Playgroud)
Geo*_*zov 25
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable);
Run Code Online (Sandbox Code Playgroud)
Chi*_*ani 18
试试这个动态代码
String fnm = "cat"; // this is image file name
String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+fnm , null, null);
System.out.println("IMG ID :: "+imgId);
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
your_image_view.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId));
Run Code Online (Sandbox Code Playgroud)
在上面的代码,你需要图片文件名称和图像的视图对象这两者你有.
看下面的代码,这对我有用
iv.setImageResource(getResources().getIdentifier(
"imagename", "drawable", "com.package.application"));
Run Code Online (Sandbox Code Playgroud)
这对我有用(不要使用图像的扩展名,仅使用名称):
String imagename = "myImage";
int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);
Run Code Online (Sandbox Code Playgroud)
ImageView imageView=findViewById(R.id.imageView)
Run Code Online (Sandbox Code Playgroud)
如果您的可绘制文件夹中有图像,则使用
imageView.setImageResource(R.drawable.imageView)
Run Code Online (Sandbox Code Playgroud)
如果你有 uri 并想在 imageView 中显示它,那么使用
imageView.setImageUri("uri")
Run Code Online (Sandbox Code Playgroud)
如果你有位图并想在 imageView 中显示它,那么使用
imageView.setImageBitmap(bitmap)
Run Code Online (Sandbox Code Playgroud)
注意:- 1.imageView.setImageDrawable()
现在在 java 中已弃用 2. 如果图像 uri 来自 firebase 或任何其他在线链接,则使用
Picasso.get()
.load("uri")
.into(imageView)
Run Code Online (Sandbox Code Playgroud)
(https://github.com/square/picasso)
或使用
Glide.with(context)
.load("uri")
.into(imageView)
Run Code Online (Sandbox Code Playgroud)
(https://github.com/bumptech/glide)
这是今天在每部手机中运行的最佳方式.大多数答案都已弃用或需要API> = 19或22.您可以使用片段代码或活动代码取决于您的需要.
分段
//setting the bitmap from the drawable folder
Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.my_image);
//set the image to the imageView
imageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)
活动
//setting the bitmap from the drawable folder
Bitmap bitmap= BitmapFactory.decodeResource(MyActivity.this.getResources(), R.drawable.my_image);
//set the image to the imageView
imageView.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)
干杯!
归档时间: |
|
查看次数: |
171417 次 |
最近记录: |