我正在开发一个
Android应用程序,其中需要提供读写外部存储的权限.我的要求是从图库中选择一张图片并在我的应用中使用它.除了棉花糖设备,一切都很好.我想为Marshmallow提供许可.谁能帮我这个?
解决方法
它可以实现如下……
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{ private static final int REQUEST_WRITE_PERMISSION = 786; @Override public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) { if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) { openFilePicker(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestPermission(); } private void requestPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_WRITE_PERMISSION); } else { openFilePicker(); } } }