如何从 /storage/emulated/0 访问文件列表
How to get access to list of files from /storage/emulated/0
我正在编写一个 Android 应用程序,可以拍摄和观看照片。所有捕获的图像都出现在网格 RecyclerView (CameraActivity) 中,我在其中使所有元素都可以点击。为了存储图像,我在 getExternalFilesDir(Environment.DIRECTORY_PICTURES) 中使用内部存储器,因为我想让它们只能从我的应用程序访问。结果我的图片文件夹的完整路径是 /storage/emulated/0/Android/data/com._,_/files/Pictures/image gallery。
在我的应用程序的第二个 activity 中,我想在图像之间滑动 为此,我需要从上面的存储目录中获取图像路径列表。 但我的问题是我不知道该怎么做。我尝试使用 getDir("image gallery", 0) 但它只是创建了一个路径为 /data/data/com.webartil.[= 的新文件夹28=]画廊。所以我需要你的帮助来解决我的问题。
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);
}
}
第一:提到的目录路径可供所有应用程序访问。
其次:列表可以用new File(path).listFiles();
完成
我正在编写一个 Android 应用程序,可以拍摄和观看照片。所有捕获的图像都出现在网格 RecyclerView (CameraActivity) 中,我在其中使所有元素都可以点击。为了存储图像,我在 getExternalFilesDir(Environment.DIRECTORY_PICTURES) 中使用内部存储器,因为我想让它们只能从我的应用程序访问。结果我的图片文件夹的完整路径是 /storage/emulated/0/Android/data/com._,_/files/Pictures/image gallery。 在我的应用程序的第二个 activity 中,我想在图像之间滑动 为此,我需要从上面的存储目录中获取图像路径列表。 但我的问题是我不知道该怎么做。我尝试使用 getDir("image gallery", 0) 但它只是创建了一个路径为 /data/data/com.webartil.[= 的新文件夹28=]画廊。所以我需要你的帮助来解决我的问题。 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);
}
}
第一:提到的目录路径可供所有应用程序访问。
其次:列表可以用new File(path).listFiles();