要获取ImageView中Bitmap的文件路径,可以使用以下方法:
ImageView imageView = findViewById(R.id.imageView);
// 获取ImageView中的Drawable
Drawable drawable = imageView.getDrawable();
// 将Drawable转换为Bitmap
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// 创建文件路径
String filePath = Environment.getExternalStorageDirectory().getPath() + "/image.jpg";
// 创建文件输出流
OutputStream outputStream = new FileOutputStream(filePath);
// 将Bitmap写入文件
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
// 关闭文件输出流
outputStream.close();
// 获取文件路径
Log.d("FilePath", filePath);
首先,添加Glide库的依赖。在app/build.gradle文件中的dependencies部分添加以下代码:
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
然后,使用Glide库加载ImageView中的图片,并获取文件路径:
ImageView imageView = findViewById(R.id.imageView);
Glide.with(this)
.asFile()
.load(imageView)
.into(new SimpleTarget() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable Transition super File> transition) {
// 获取文件路径
String filePath = resource.getAbsolutePath();
Log.d("FilePath", filePath);
}
});
这样就可以获取ImageView中Bitmap的文件路径了。