java - 显示从图库中选择的 ImagePreview 数据
问题描述
通过 ImageButton 选择图像然后在同一位置显示到图像预览,我可以从图库中选择图像但问题是当我想在 ImageButton 的同一位置显示一些预览时,但图像预览来自 ArrayList 项目,如果我将在activity_the_grid中检查示例图像以检查布局并给我[java.lang.RuntimeException:失败交付结果ResultInfo]错误 选择图像,我可以将图像传递给一个
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_grid);
dgridPreviewImage = findViewById(R.id.the_grid_image_preview); //layout_item
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
SectionedExpandableLayoutHelper sectionedExpandableLayoutHelper = new SectionedExpandableLayoutHelper(this,
mRecyclerView, this, 3);
//random data
ArrayList<Item> arrayList = new ArrayList<>();
arrayList.add(new Item(0));
arrayList.add(new Item( 1));
arrayList.add(new Item( 2));
arrayList.add(new Item( 3));
arrayList.add(new Item( 4));
arrayList.add(new Item( 5));
sectionedExpandableLayoutHelper.notifyDataSetChanged();
}
@Override
public void itemClicked(Item item) {
openMediaSelectDialog();
}
private void handleImageSelect(@Nullable Intent intent) {
if (saveContentLocally(intent)) {
reset();
try {
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(mSnapFile));
dgridPreviewImage.setImageBitmap(bitmap);
mSnapState = TheGrid.SnapState.IMAGE;
} catch (FileNotFoundException e) {
throw new IllegalStateException("Saved the image file, but it doesn't exist!");
}
}
}
物品
public class Item {
private final int id;
public Item( int id) {
this.id = id;
}
public int getId() {
return id;
}
布局项目
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_margin="@dimen/item_margin"
android:orientation="vertical"
>
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
card_view:cardElevation="10dp"
android:layout_margin="@dimen/item_margin"
android:layout_centerHorizontal="true"
android:layout_width="500dp"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="@drawable/select_snap" />
<ImageView
android:id="@+id/the_grid_image_preview"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerInside" />
<com.snapchat.kit.chickencam.SnapVideoView
android:id="@+id/the_grid_video_preview"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:visibility="gone" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<Button
android:id="@+id/thegrid_share_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear"
android:layout_below="@id/card_view"
/>
</RelativeLayout>
解决方案
您可以从本地路径获取图像
object ImagesGallery {
fun listOfImages(context : Context) :ArrayList<GalleryImage> {
val cursor : Cursor
val column_index_data : Int
val listOfAllImages : ArrayList<GalleryImage> = ArrayList()
var absolutePathOfImage : String
val uri : Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME)
val orderBy : String = MediaStore.Images.Media.DATE_TAKEN
cursor = context.contentResolver.query(uri, projection, null, null, orderBy + " DESC")!!
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
while (cursor.moveToNext()){
absolutePathOfImage = cursor.getString(column_index_data)
listOfAllImages.add(GalleryImage(absolutePathOfImage, false))
}
return listOfAllImages
}
}
然后我们可以在 MainActivity 中调用 listOfImages()
class MainActivity : AppCompatActivity(){
ArrayList<String> images = new ArrayList();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
images = ImagesGallery.listOfImages(this)
}
}
推荐阅读
- gradle - 使用 Kotlin 实现插件时如何为 Gradle 扩展任务?
- python - json文件中布尔数据类型的反序列化在python中不起作用
- c++ - 将 lambda 传递给可变参数 std::function 时的类型推导
- laravel - 在运行时在集合上添加属性追加
- android - 签名的 React Native 应用程序未正确加载资产
- android - React-Native:如何使用模糊创建 UI
- botframework - Microsoft 签名密钥不再有背书
- wordpress - 我的 .htaccess 文件与通过 Filezilla 上传的 301 重定向给出 500 内部服务器错误有什么问题?
- excel - 宏提示我选择文件,即使它打开
- c# - 没有外键的实体框架主从数据输入