android pick images from gallery

前端 未结 10 1712
太阳男子
太阳男子 2020-11-22 13:53

I want to create a picture chooser from gallery. I use code

 intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTE         


        
相关标签:
10条回答
  • 2020-11-22 14:24
    public void FromCamera() {
    
        Log.i("camera", "startCameraActivity()");
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 1);
    
    }
    
    public void FromCard() {
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, 2);
    }
    
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == 2 && resultCode == RESULT_OK
                && null != data) {
    
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
    
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
    
            bitmap = BitmapFactory.decodeFile(picturePath);
            image.setImageBitmap(bitmap);
    
            if (bitmap != null) {
                ImageView rotate = (ImageView) findViewById(R.id.rotate);
    
            }
    
        } else {
    
            Log.i("SonaSys", "resultCode: " + resultCode);
            switch (resultCode) {
            case 0:
                Log.i("SonaSys", "User cancelled");
                break;
            case -1:
                onPhotoTaken();
                break;
    
            }
    
        }
    
    }
    
    protected void onPhotoTaken() {
        // Log message
        Log.i("SonaSys", "onPhotoTaken");
        taken = true;
        imgCapFlag = true;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        bitmap = BitmapFactory.decodeFile(path, options);
        image.setImageBitmap(bitmap);
    
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:30

    Sometimes, you can't get a file from the picture you choose. It's because the choosen one came from Google+, Drive, Dropbox or any other provider.

    The best solution is to ask the system to pick a content via Intent.ACTION_GET_CONTENT and get the result with a content provider.

    You can follow the code bellow or look at my updated gist.

    public void pickImage() {
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("image/*");
      startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
            if (data == null) {
                //Display an error
                return;
            }
            InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
            //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:33

    Absolutely. Try this:

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    

    Don't forget also to create the constant PICK_IMAGE, so you can recognize when the user comes back from the image gallery Activity:

    public static final int PICK_IMAGE = 1;
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == PICK_IMAGE) {
            //TODO: action
        }
    }
    

    That's how I call the image gallery. Put it in and see if it works for you.

    EDIT:

    This brings up the Documents app. To allow the user to also use any gallery apps they might have installed:

        Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
        getIntent.setType("image/*");
    
        Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        pickIntent.setType("image/*");
    
        Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
    
        startActivityForResult(chooserIntent, PICK_IMAGE);
    
    0 讨论(0)
  • 2020-11-22 14:38

    Just to offer an update to the answer for people with API min 19, per the docs:

    On Android 4.4 (API level 19) and higher, you have the additional option of using the ACTION_OPEN_DOCUMENT intent, which displays a system-controlled picker UI controlled that allows the user to browse all files that other apps have made available. From this single UI, the user can pick a file from any of the supported apps.

    On Android 5.0 (API level 21) and higher, you can also use the ACTION_OPEN_DOCUMENT_TREE intent, which allows the user to choose a directory for a client app to access.

    Open files using storage access framework - Android Docs

         val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
         intent.type = "image/*"
         startActivityForResult(intent, PICK_IMAGE_REQUEST_CODE)
    
    0 讨论(0)
提交回复
热议问题