I'm writing an Android application, which can capture and watch photos. All captured images appear in grid RecyclerView (CameraActivity), where I made all elements are clickable. To store images I'm using internal memory within getExternalFilesDir(Environment.DIRECTORY_PICTURES), because I want to make them accessible only from my application. In the result the full path of my picture folder is /storage/emulated/0/Android/data/com._,_/files/Pictures/image gallery. In the second activity of my app I want to slide between images and for this I need to get the list of image paths from above storage directory. But my problem is that I don't know how to do this. I've tried to use getDir("image gallery", 0) but it simply creates a new folder with path /data/data/com.webartil.cameraapp/app_image gallery. So I need your help to solve my problem. CameraActivity
public class CameraActivity extends AppCompatActivity implements ImageAdapter.OnImageClickListener {
private static final int ACTIVITY_START_CAMERA_APP = 0;
public static final String PATH = "PATH";
private String GALLERY_LOCATION = "image gallery";
private File mGalleryFolder;
private RecyclerView mRecyclerView;
private ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
createImageGallery();
Toolbar toolbar = findViewById(R.id.toolbar_activity_camera);
setSupportActionBar(toolbar);
initRecyclerView();
FloatingActionButton fab = findViewById(R.id.fab_shoot_photo);
fab.setOnClickListener(view -> takePhoto());
}
private void initRecyclerView() {
mRecyclerView = findViewById(R.id.gallery_recycler_view);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3);
mRecyclerView.setLayoutManager(layoutManager);
imageAdapter = new ImageAdapter(this, mGalleryFolder, this);
mRecyclerView.setAdapter(imageAdapter);
}
public void takePhoto() {
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
RecyclerView.Adapter newImageAdapter = new ImageAdapter(this, mGalleryFolder, this);
mRecyclerView.swapAdapter(newImageAdapter, false);
}
File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMAGE_" + timeStamp + "_";
return File.createTempFile(imageFileName,".jpg", mGalleryFolder);
}
private void createImageGallery() {
mGalleryFolder =
new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES),
GALLERY_LOCATION);
if(!mGalleryFolder.exists()) {
mGalleryFolder.mkdirs();
}
Log.d(PATH, mGalleryFolder.getAbsolutePath());
}
@Override
public void onClickImage(final View view, final int position) {
Intent intent = new Intent(this, ImageActivity.class);
intent.putExtra(PATH, position);
startActivity(intent);
}
}
First: mentioned directory path is accessible for all apps.
Second: listings can be done with new File(path).listFiles();