有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在MediaStore中分离来自SD卡和设备内部内存的图像

我试图访问来自MediaStore的所有图像,但我想将来自它的图像分成两个不同的文件夹;外部内存和内部内存,其中内部内存是Android设备的主存储,外部内存类似于SD卡。我怎样才能做到这一点

我的代码如下

我有以下活动:

 package com.projects.timely.gallery;
    
    import 安卓.content.Intent;
    import 安卓.os.Bundle;
    import 安卓.os.Environment;
    import 安卓.view.View;
    import 安卓.view.ViewGroup;
    
    import 安卓x.annotation.Nullable;
    import 安卓x.appcompat.app.AppCompatActivity;
    
    import com.projects.timely.R;
    
    @SuppressWarnings("ConstantConditions")
    public class StorageViewer extends AppCompatActivity implements View.OnClickListener {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_storage_view);
            setSupportActionBar(findViewById(R.id.toolbar));
            getSupportActionBar().setTitle("Select Storage");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            ViewGroup v_internalStorage = findViewById(R.id.internal_storage);
            v_internalStorage.setOnClickListener(this);
    
            ViewGroup v_externalStorage = findViewById(R.id.external_storage);
            v_externalStorage.setOnClickListener(this);
    
            boolean ext_str_mounted
                    = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
            v_externalStorage.setVisibility(ext_str_mounted ? View.VISIBLE : View.GONE);
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.internal_storage) {
                startActivity(new Intent(this, ImageDirectory.class)
                        .putExtra(ImageDirectory.STORAGE_ACCESS_ROOT, ImageDirectory.INTERNAL));
            } else {
                startActivity(new Intent(this, ImageDirectory.class)
                        .putExtra(ImageDirectory.STORAGE_ACCESS_ROOT, ImageDirectory.EXTERNAL));
            }
            finish();
        }
    
        @Override
        public boolean onSupportNavigateUp() {
            super.onBackPressed();
            return true;
        }
    }

这将触发此代码。此代码在活动中执行。它位于名为ImageDirectory的文件中。java

 @Override
        @SuppressLint("InlinedApi")
        public void run() {
            String root_extra = getIntent().getStringExtra(STORAGE_ACCESS_ROOT);
            Uri storageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            if (root_extra != null) {
                storageUri = root_extra.equals(EXTERNAL) ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                        : MediaStore.Images.Media.INTERNAL_CONTENT_URI;
            }
    
            String[] projection = {
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.DISPLAY_NAME};
            Cursor imgCursor = getApplicationContext()
                    .getContentResolver()
                    .query(storageUri, projection, null, null, null);
    
            int bucketId = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            int imgSize = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
            int name = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
            int bucketName
                    = imgCursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
    
            List<String> dirName = new ArrayList<>();
            while (imgCursor.moveToNext()) {
                long id = imgCursor.getLong(bucketId);
                int size = imgCursor.getInt(imgSize);
                String fileName = imgCursor.getString(name);
                String folderName = imgCursor.getString(bucketName);
    
// Updated this part, replaced with storageUri

                 Uri contentUri
                    = ContentUris.withAppendedId(storageUri, id);
                Image currentImage = new Image(contentUri, size, fileName, folderName);
    
                int directoryIndex = linearSearch(dirName, folderName);
                // if search result (directoryIndex) passes this test, then it means that there is
                // no such directory in list of directory names
                if (directoryIndex < 0) {
                    imageDirectoryList.add(new ArrayList<>());
                    dirName.add(folderName);
                    directoryIndex = linearSearch(dirName, folderName);
                    if (directoryIndex >= 0)
                        imageDirectoryList.get(directoryIndex).add(currentImage);
                } else {
                    imageDirectoryList.get(directoryIndex).add(currentImage);
                }
            }
    
            imgCursor.close();
            runOnUiThread(() -> {
                imageAdapter.notifyDataSetChanged();
                doViewUpdate();
            });
    
        }

共 (0) 个答案