显示来自画廊的图像预览所选数据

huangapple go评论78阅读模式
英文:

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&lt;Item&gt; arrayList = new ArrayList&lt;&gt;();
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(&quot;Saved the image file, but it doesn&#39;t exist!&quot;);
}
}
}
**Item**
public class Item {
private final int id;
public Item( int id) {
this.id = id;
}
public int getId() {
return id;
}
**layout_item**
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
xmlns:card_view=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:padding=&quot;3dp&quot;
android:layout_margin=&quot;@dimen/item_margin&quot;
android:orientation=&quot;vertical&quot;
&gt;
&lt;androidx.cardview.widget.CardView
android:id=&quot;@+id/card_view&quot;
card_view:cardElevation=&quot;10dp&quot;
android:layout_margin=&quot;@dimen/item_margin&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_width=&quot;500dp&quot;
android:layout_height=&quot;wrap_content&quot;
&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;ImageView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;120dp&quot;
android:layout_gravity=&quot;center&quot;
android:src=&quot;@drawable/select_snap&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/the_grid_image_preview&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;120dp&quot;
android:scaleType=&quot;centerInside&quot; /&gt;
&lt;com.snapchat.kit.chickencam.SnapVideoView
android:id=&quot;@+id/the_grid_video_preview&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;120dp&quot;
android:layout_gravity=&quot;center&quot;
android:visibility=&quot;gone&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;Button
android:id=&quot;@+id/thegrid_share_button&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Clear&quot;
android:layout_below=&quot;@id/card_view&quot;
/&gt;
&lt;/RelativeLayout&gt;
[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&lt;GalleryImage&gt; {
val cursor : Cursor
val column_index_data : Int
val listOfAllImages : ArrayList&lt;GalleryImage&gt; = 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 + &quot; DESC&quot;)!!
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&lt;String&gt; images = new ArrayList();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
images = ImagesGallery.listOfImages(this)
}
}

huangapple
  • 本文由 发表于 2020年9月26日 14:40:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64074673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定