0

Моя грамматика может работать на Android версии 5.1, но не работает на андроид 7.1 ....Как отобразить изображение с intent.ACTION_VIEW

File file = new File(Environment.getExternalStorageDirectory(), "Pictures/1481853170451.jpg"); 
Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show(); 
Uri path = Uri.fromFile(file); 

if (file.exists()) { 
    Intent intent = new Intent(); 
    intent.setAction(android.content.Intent.ACTION_VIEW); 
    intent.setDataAndType(path, "image/*"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
try {      
    this.startActivity(intent); 
} catch (ActivityNotFoundException e) { 
} 

Может ли один скажите мне возможный ответ. Спасибо заранее ....

+0

Android выдачу разрешений, регистрация разрешения выполнения для внешнего хранения чтения –

+1

Возможной дубликаты [Android Error 6.0 Разрешения] (http://stackoverflow.com/questions/33078003/android-6-0-permission-error) –

ответ

0

Просто пройти через следующую link

0

Попробуйте это,

разрешения камеры

private static final int REQUEST_STORAGE = 112; 


    btn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (Build.VERSION.SDK_INT >= 23) { 
         String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}; 
         if (!hasPermissions(mContext, PERMISSIONS)) { 
          ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_STORAGE); 
         } else { 
          getImage(); 
         } 
        } else { 
         getImage(); 
        } 

       } 
      }); 
     } 

получить разрешение Результата

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     switch (requestCode) { 
      case REQUEST_STORAGE: { 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        getImage(); 
       } 
      } 
     } 
    } 

проверки прав доступа для зефира

private static boolean hasPermissions(Context context, String... permissions) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) { 
      for (String permission : permissions) { 
       if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { 
        return false; 
       } 
      } 
     } 
     return true; 
    } 

Тека изображение от внешнего хранения

public void getImage() 
    { 
     File file = new File(Environment.getExternalStorageDirectory(), "Pictures/1481853170451.jpg"); 
     Toast.makeText(MainActivity.this, file.getPath(), Toast.LENGTH_LONG).show(); 
     Uri path = Uri.fromFile(file); 

     if (file.exists()) { 
      Intent intent = new Intent(); 
      intent.setAction(android.content.Intent.ACTION_VIEW); 
      intent.setDataAndType(path, "image/*"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     try {      
      this.startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
     } 
    }  

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />