gpt4 book ai didi

android - 应用程序是否需要权限才能打开相机和图库

转载 作者:行者123 更新时间:2023-11-29 16:54:29 26 4
gpt4 key购买 nike

我曾经认为要从您的应用程序访问相机和画廊,需要权限,特别是在 Marshmallow 或运行时需要更高权限

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

但我认为我错了,因为我的应用程序也在未经许可的情况下运行。我有 Myapp -> 许可。未授予任何权限。 但它是如何工作的。下面是我的画廊简单代码:

 @Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
takePhoto ( );
}

public void takePhoto ( ) {
Intent intent = new Intent ( );
intent.setType ( "image/*" );
intent.setAction ( Intent.ACTION_GET_CONTENT );//
startActivityForResult ( Intent.createChooser ( intent, "Select Image" ), SELECT_IMAGE );
}

public void onActivityResult ( int requestCode, int resultCode, Intent data ) {
super.onActivityResult ( requestCode, resultCode, data );
if ( requestCode == SELECT_IMAGE ) {
if ( resultCode == Activity.RESULT_OK ) {
if ( data != null ) {
try {
Log.e ( "INSIDE------", "onActivityResult: " );
Bitmap bitmap = MediaStore.Images.Media.getBitmap ( this.getContentResolver ( ), data.getData ( ) );

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

}
} else if ( resultCode == Activity.RESULT_CANCELED ) {
Toast.makeText ( this, "Cancelled", Toast.LENGTH_SHORT ).show ( );
}
}
}

对于相机,这是代码:

 @Override
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
takePhoto ( );
}
public void takePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, TAKE_PICTURE);
}

我的 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.utkarshshukla.fragmentpractice">

<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

</manifest>

我的 Gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.utkarshshukla.fragmentpractice"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:26.+'
testCompile 'junit:junit:4.12'
}

这两个代码都有效,我没有在 list 中声明任何权限。虽然在 list 中为相机声明许可后它崩溃了,说需要许可但没有在 list 中写它为什么有效(没有为画廊尝试相同)。在开发者网站上他们提到要获得许可但没有提到如果会发生什么未经许可。
问题是为什么它在没有权限的情况下工作

谢谢

最佳答案

根据 Consider Using an Intent training :

In many cases, you can choose between two ways for your app to perform a task. You can have your app ask for permission to perform the operation itself. Alternatively, you can have the app use an intent to have another app perform the task.

ACTION_IMAGE_CAPTUREACTION_GET_CONTENT 都是使用 Intent 请求另一个应用程序或系统向您的应用程序提供内容而无需任何请求的示例运行时权限。因此,这些操作不需要运行时权限。

关于android - 应用程序是否需要权限才能打开相机和图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45384695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com