0

i want display image from Gallery to ImageView. in emulator Android 4.2.2, my code can be running as I expected. but when I plug in the smartphone Android 6.0.1 cant be display the Image to ImageView this is my code :

//Open gallery code
private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }


//Set ImageView code
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            filePath = data.getData();
            try {
                imageView = (ImageView) getActivity().findViewById(R.id.imageView);
                bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
    }

My question. Is this because of my android version? and how to solve this solution? Thanks

Bhushan
  • 41
  • 5
Sate Wedos
  • 539
  • 8
  • 20

3 Answers3

0

This link will help you. Just Add ImageFilePath.java in your project and it will solve your problem.

0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app

if (ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    // User may have declined earlier, ask Android if we should show him a reason

                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
                        // show an explanation to the user
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE);

                        // Good practise: don't block thread after the user sees the explanation, try again to request the permission.
                    } else {
                        // request the permission.
                        // CALLBACK_NUMBER is a integer constants
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE);
                        // The callback method gets the result of the request.
                    }
                } else {

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

                }

In onActivityResult(int requestCode, int resultCode, Intent data)

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

        if(requestCode==12)
        {
            if(resultCode==RESULT_OK)
            {

                if (data == null) {
                    //Display an error
                    Utils.Toast_S(getActivity(), "No Image Data");

                    return;
                }
                try {
                     inputStream = getContext().getContentResolver().openInputStream(data.getData());

                    imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));

                    File  file=new File("");
                    file.getName();
                    file.getAbsolutePath();



                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), "Exception", Toast.LENGTH_SHORT).show();
                }


            }
        }

    }
Community
  • 1
  • 1
Mallikarjuna
  • 874
  • 6
  • 17
  • You are right. after i give permission, i can take photo from gallery. Thank you very much – Sate Wedos Oct 26 '17 at 07:43
  • but i have a question. what code do you write for permissions for the camera? my code succeeds after I replace with "READ_EXTERNAL_STORAGE". funds why android 6.0.1 I do not ask permission to the camera? – Sate Wedos Oct 26 '17 at 08:02
0

System permissions are divided into several protection levels. The two most important protection levels to know about are normal and dangerous permissions:

Normal permissions cover areas where your app needs to access data or resources outside the app's sandbox, but where there's very little risk to the user's privacy or the operation of other apps. For example, permission to set the time zone is a normal permission. If an app declares that it needs a normal permission, the system automatically grants the permission to the app. For a full listing of the current normal permissions, see Normal permissions.

Dangerous permissions cover areas where the app wants data or resources that involve the user's private information, or could potentially affect the user's stored data or the operation of other apps. For example, the ability to read the user's contacts is a dangerous permission. If an app declares that it needs a dangerous permission, the user has to explicitly grant the permission to the app:

[LInk]https://developer.android.com/guide/topics/permissions/requesting.html

Mallikarjuna
  • 874
  • 6
  • 17