android 获取imageview图片
时间: 2023-05-01 21:05:07 浏览: 71
获取 ImageView 图片,您需要使用以下代码:
ImageView imageView = findViewById(R.id.yourImageViewId);
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
相关问题
Android怎么获取imageview的图片路径
你可以使用以下代码获取ImageView中的图片路径:
```java
ImageView imageView = findViewById(R.id.imageView);
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Image Description", null);
Uri uri = Uri.parse(path);
String imagePath = getRealPathFromURI(uri);
```
其中,getRealPathFromURI()方法可以通过Uri获取真实路径。
Android ImageView显示本地图片
要在Android ImageView中显示本地图片,可以按照以下步骤:
1. 将图片文件保存在应用程序的资源文件夹中(例如:res/drawable)。
2. 在XML布局文件中添加ImageView控件,并设置android:src属性为本地图片的名称,例如:
```
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_image" />
```
3. 在Java代码中获取ImageView控件的引用,并设置本地图片。例如:
```
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.my_image);
```
注意:如果图片文件较大,可能会导致应用程序的内存占用过高,建议使用图片加载库(例如Glide、Picasso)来加载图片。