棉花糖的存储和图库使用的读写权限

时间:2016-03-08 04:29:24

标签: android android-permissions android-6.0-marshmallow read-write

我正在开发一个Android应用程序,其中需要提供读写外部存储的权限。我的要求是从图库中选择一张图片并在我的应用中使用它。除了棉花糖设备外,一切正常。我想为Marshmallow提供许可。任何人都可以帮我这个吗?

6 个答案:

答案 0 :(得分:19)

可以通过以下方式实现......

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();
    }
  }
}

答案 1 :(得分:2)

通读Runtime permissions - Android Mits reference project

权限流已更改,需要在运行时询问权限,最好是在需要时。使用相同的..权限也被归类为Normal and Dangerous,这也将导致权限组。

答案 2 :(得分:1)

要在存储中写入文件, 首先,需要在 manifest.xml 中添加写权限,

然后, 在Android 6中,它包含某些任务的权限系统。每个应用程序都可以在运行时请求所需的权限。因此,我们打开“活动”类,在下面添加代码以请求运行时权限。

使用以下命令检查您的应用程序是否具有运行时写入权限

override fun onRequestPermissionsResult(requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray) {

    when(requestCode)  {

        100 -> {
            if (grantResults.size > 0 && permissions[0] == Manifest.permission.WRITE_EXTERNAL_STORAGE) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                
                }
            }

        }
    }

}

答案 3 :(得分:0)

此外,如果您想要多个权限。你可以这样做。

private ArrayList<String> permisos;

private void someMethod()
{
      ..

      verificarPermisos(Manifest.permission.READ_EXTERNAL_STORAGE );
      verificarPermisos(Manifest.permission.ACCESS_FINE_LOCATION );
      verificarPermisos(Manifest.permission.ACCESS_COARSE_LOCATION );
      if (permisos.size()!=0)
      {
         solicitarPermisos(permisos.toArray(new String[permisos.size()]));
      }

      ..

 }


@TargetApi(23)
void verificarPermisos(String permiso){
    if (ContextCompat.checkSelfPermission(this,permiso)
            != PackageManager.PERMISSION_GRANTED) {
        permisos.add(permiso);
        // Should we show an explanation?
        if (shouldShowRequestPermissionRationale(permiso)) {

        }

    }
}

@TargetApi(23)
void solicitarPermisos(String[] permiso){

    requestPermissions(permiso, 1);

}

首先,我使用“ verificarPermisos()”方法检查是否已授予权限。如果他们没有被授予,我将他们添加到“ permisos ”arraylist。最后,请求未授权的权限以 String [] 数组格式传递它们。

请注意,您只能在MashMellow(API 23)或更高版本之后请求权限。因此,如果你的应用程序也在MashMellow之前定位Android版本,你可以使用“ @TargetApi()”注释,只有当目标是API 23或更高版本时才会调用权限方法。
希望能得到帮助,问候。

答案 4 :(得分:0)

检查每种情况

  

如果被拒绝 - 向用户显示警告对话框,为什么我们需要许可

public static final int STORAGE_PERMISSION_REQUEST_CODE= 1;


    private void askPermissions() {

    int permissionCheckStorage = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE); 

   // we already asked for permisson & Permission granted, call camera intent
    if (permissionCheckStorage == PackageManager.PERMISSION_GRANTED) {

        //do what you want

    } else {

           // if storage request is denied
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("You need to give permission to access storage in order to work this feature.");
            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            builder.setPositiveButton("GIVE PERMISSION", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();

                    // Show permission request popup
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            STORAGE_PERMISSION_REQUEST_CODE);
                }
            });
            builder.show();

        } //asking permission for first time 
          else {
             // Show permission request popup for the first time
                        ActivityCompat.requestPermissions(AddWorkImageActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                STORAGE_PERMISSION_REQUEST_CODE);

                    }

    }
}

检查权限结果

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {
        case STORAGE_PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // check whether storage permission granted or not.
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //do what you want;
                }
            }
            break;
        default:
            break;
    }
}
  

你可以复制并粘贴这段代码,它运行正常。改变背景(这)&amp;根据你的权限。

答案 5 :(得分:0)

检查AndroidManifest.xml!

    <?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.yoganetwork">
<!-- Internet Permission -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.YogaNetwork">
    <activity android:name=".LoginActivity"></activity>
    <activity android:name=".DashboardActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" 
/>

            <category 
android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".RegisterActivity" />
    <activity android:name=".MainActivity" />
</application>

</manifest>