简体   繁体   中英

How to add images in your app from gallery into recyclerview in android?

My app contains a section where I have to load images from gallery and show them there. Please guide me how I can do that. I also want to make folders in which the photos will be placed. Thanks in advance for your help!

Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery,PICK_IMAGE);

I've tried the above code it selects the image and shows it on my app but when I restart my app the image disappears.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        imageView.setImageURI(imageUri);
    }
}
 final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);

                Bitmap newPicture;

                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inJustDecodeBounds = true;

                // Decode the image file into a Bitmap sized to fill the View
                bmOptions.inJustDecodeBounds = false;
                bmOptions.inPurgeable = true;

                newPicture = BitmapFactory.decodeStream(imageStream);


                myPicturesArrayList.add(newPicture);

                picturesAdapter = new MyPicturesAdapter(myPicturesArrayList); //Your custom adapter
                myRecyclerView.setAdapter(picturesAdapter );

在这种情况下,您必须首先将图像保存在本地或服务器上的数据库中......下次从数据库中获取

After you load the image from gallery, save the image Uri in SharedPreferences. Next time load it directly (Get the uri from the Sharedpreference). For details pls refer Android Shared preferences example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM