07
Дек
2017

Установить картинку через галерею с помошью permission Android studio

нужно при нажатии кнопки установить imageView из галереи подскажите что не так с кодом, выскакивает ошибка при нажатии кнопки

public class MainActivity extends AppCompatActivity {
    Button button;
    ImageView imageView;
    private final  int REQUEST_IMAGE_CAPTURE = 3;
    private final  int RESULT_LOAD_IMAGE = 2;
    static final int REQUEST_TAKE_PHOTO = 1;

    String mCurrentPhotoPath;

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

        button = findViewById(R.id.button);
        imageView = findViewById(R.id.imageView);



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dispatchTakePictureIntent();

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {   //проверка для андроид 5...
                    /*takePic();*/

                    return;
                }

                if (ContextCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        == PackageManager.PERMISSION_GRANTED) {

                }else{
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 13);

                }

            }

        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent takePictureIntent) {
        super.onActivityResult(requestCode, resultCode, takePictureIntent);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != takePictureIntent) {
            Uri selectedImage = takePictureIntent.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            assert selectedImage != null;
            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            assert cursor != null;
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 13: {

                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                }else{

                }return;

            }
        }
    }

    private File createImageFile() throws IOException{

        String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storegeDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName, ".jpg", storegeDir);

        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {

            }
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "application.com.decemper06.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }
}

Источник: https://ru.stackoverflow.com/questions/754946/%D0%A3%D1%81%D1%82%D0%B0%D0%BD%D0%BE%D0%B2%D0%B8%D1%82%D1%8C-%D0%BA%D0%B0%D1%80%D1%82%D0%B8%D0%BD%D0%BA%D1%83-%D1%87%D0%B5%D1%80%D0%B5%D0%B7-%D0%B3%D0%B0%D0%BB%D0%B5%D1%80%D0%B5%D1%8E-%D1%81-%D0%BF%D0%BE%D0%BC%D0%BE%D1%88%D1%8C%D1%8E-permission-android-studio

Тебе может это понравится...

Добавить комментарий