2016-03-30 9 views
5

używam w ten sposób, aby otworzyć próbnik do obrazuApp ulega awarii podczas otwierania obrazu próbnik w prawoślazu

Intent intent = new Intent(); 
// Show only images, no videos or anything else 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
// Always show the chooser (if there are multiple options available) 
startActivityForResult(Intent.createChooser(intent, "Select Picture"),  PICK_IMAGE_REQUEST); 
+0

trzeba prosić o pozwolenie na Marshmellow. –

Odpowiedz

4

trzeba prosić o pozwolenie do odczytu pamięci zewnętrznej

public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 23; 

Zapytaj o zgodę, zanim zadzwonisz odebrać obraz

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 
     } 
    } 

Sprawdź Udziela się zgody lub nie

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      } else { 

      } 
      return; 
     } 
    } 
} 
2

Spróbuj tak ....

if (Build.VERSION.SDK_INT <= 19) { 
           Intent intent = new Intent(); 
           intent.setType("image/*"); 
           intent.setAction(Intent.ACTION_GET_CONTENT); 
           intent.addCategory(Intent.CATEGORY_OPENABLE); 
           startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 
          } else if (Build.VERSION.SDK_INT > 19) { 
           Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
           startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 


      } 

Tested.It pewnością pomóc. ..

Powiązane problemy