英文:
Display ImagePreview Selected Data from Gallery
问题
选择图像后,通过ImageButton在相同位置显示图像预览,我可以从图库中选择图像,但问题是当我想在ImageButton的相同位置显示一些预览图像时,图像预览来自ArrayList项目,如果我将在activity_the_grid中执行,可以将图像传递给其中一个,检查示例图像以检查布局,并给我一个 [java.lang.RuntimeException: Failure delivering result ResultInfo] 错误
MainActivity:
@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("已保存图像文件,但文件不存在!");
}
}
}
Item:
public class Item {
private final int id;
public Item(int id) {
this.id = id;
}
public int getId() {
return id;
}
layout_item:
<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>
[示例图像][1]
[1]: https://i.stack.imgur.com/Ftf6g.png
<details>
<summary>英文:</summary>
Selecting the image via the ImageButton then display to image preview in the same position, I can select the images from the gallery but the problem is when I want to display some preview on the same position of ImageButton, but the image preview is from ArrayList items, I can pass the image in one if I will do in the activity_the_grid check the sample image to check the layout and giving me [ java.lang.RuntimeException: Failure delivering result ResultInfo] error
[Select Image ][1]
**MainActivity**
@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!");
}
}
}
**Item**
public class Item {
private final int id;
public Item( int id) {
this.id = id;
}
public int getId() {
return id;
}
**layout_item**
<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>
[1]: https://i.stack.imgur.com/Ftf6g.png
[2]: https://i.stack.imgur.com/xFDOp.png
</details>
# 答案1
**得分**: 3
```kotlin
你可以从本地路径获取图像
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() {
var images = ArrayList<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
images = ImagesGallery.listOfImages(this)
}
}
英文:
You can get image from local path
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
}
}
and then we can call listOfImages() in MainActivity
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论