簡體   English   中英

如何從/ 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 在我的應用程序的第二個活動中,我想在圖像之間滑動,為此,我需要從存儲目錄上方獲取圖像路徑列表。 但是我的問題是我不知道該怎么做。 我嘗試使用getDir(“ image gallery”,0),但它只是使用路徑/data/data/com.webartil.cameraapp/app_image gallery創建一個新文件夾。 因此,我需要您的幫助來解決我的問題。 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM