0

I'm referencing this link for showing images from a specific path in internal storage. But its only working with ACTION_VIEW but when I use ACTION_PICK its not showing that same path instead its simply opening gallery. Is there any way to select a single image from that specific path after showing?

public class NewActivity extends AppCompatActivity{

    private static final String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/fun";
    private File[] allFiles ;
    private String imagepath ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File folder = new File(file_path);
        allFiles = folder.listFiles();

        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new SingleMediaScanner(NewActivity.this, allFiles[0]);

            }
        });
    }

    public class SingleMediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {

        private MediaScannerConnection mMs;
        private File mFile;

        public SingleMediaScanner(Context context, File f) {
            mFile = f;
            mMs = new MediaScannerConnection(context, this);
            mMs.connect();
        }

        public void onMediaScannerConnected() {
            mMs.scanFile(mFile.getAbsolutePath(), null);
        }

        public void onScanCompleted(String path, Uri uri) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setData(uri);
            startActivityForResult(intent, 100);
            mMs.disconnect();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==100 && resultCode==RESULT_OK){
            try{
                Uri selectedImageUri = data.getData();
                String[] projection = {MediaStore.Images.Media.DATA};
                //Cursor cursor_gallery = activity.managedQuery(selectedImageUri, projection, null, null, null);
                Cursor cursor_gallery = getContentResolver().query(selectedImageUri, projection, null, null, null);
                int column_index = 0;
                if (cursor_gallery != null) {
                    column_index = cursor_gallery.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor_gallery.moveToFirst();
                    imagepath = cursor_gallery.getString(column_index);
                    cursor_gallery.close();
                }
                System.out.println("sammy_imagepath "+imagepath);

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
Sammy
  • 181
  • 3
  • 18

1 Answers1

0

When you make an ACTION_PICK intent, you're asking another application to perform an action (let them pick a file). There is no way to ensure it works a certain way- the user could just as easily have added another app that implements ACTION_PICK and does something else. So no, there is no way to ensure it will pick from that path. If you absolutely need that, you need to implement your own picker instead of using ACTION_PICK.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • What do you mean by "implement your own picker"? How to do that? – Sammy Sep 10 '17 at 05:20
  • 1
    You write an Activity that displays all the files in the activity in a list or grid view and lets you select one. A basic one could be done in an hour or two. A good looking one will take longer, of course. – Gabe Sechan Sep 10 '17 at 05:21
  • Got it. Let me try this. – Sammy Sep 10 '17 at 05:27