在我的应用程序中,我需要在设备存储中存储大量图像.这些文件往往满足设备存储,我想让用户能够选择外部SD卡作为目标文件夹.
我到处读到Android不允许用户写入外部SD卡,SD卡是指外部和可安装的SD卡而不是外部存储器,但文件管理器应用程序设法在所有Android版本上写入外部SD.
在不同的API级别(Pre-KitKat,KitKat,Lollipop +)上授予对外部SD卡的读/写访问权限的更好方法是什么?
更新1
我从Doomknight的答案中尝试了方法1,但没有结果:正如您所看到的那样,我在尝试在SD上写入之前在运行时检查权限:
HashSet<String> extDirs = getStorageDirectories();
for(String dir: extDirs) {
Log.e("SD",dir);
File f = new File(new File(dir),"TEST.TXT");
try {
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED) {
f.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我在两个不同的设备上尝试了访问错误:HTC10和Shield K1.
10-22 14:52:57.329 30280-30280/? E/SD: /mnt/media_rw/F38E-14F8
10-22 14:52:57.329 30280-30280/? W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
10-22 14:52:57.329 30280-30280/? W/System.err: at java.io.File.createNewFile(File.java:939)
10-22 14:52:57.329 30280-30280/? W/System.err: at com.myapp.activities.TestActivity.onResume(TestActivity.java:167)
10-22 14:52:57.329 30280-30280/? W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1326)
10-22 14:52:57.330 30280-30280/? W/System.err: at android.app.Activity.performResume(Activity.java:6338)
10-22 14:52:57.330 …Run Code Online (Sandbox Code Playgroud) 摘要
最后,我发现相关问题,setForeground()错误地标记为需要在ViewGroups扩展上使用API 23 FrameLayout.
问题189041:setForeground()被错误地标记为需要扩展FrameLayout的ViewGroups的API 23(NewApi)
如果您没有扩展FrameLayout,那么文档是错误的,并且需要API 23.
问题186273:View.setForeground错误的API级别
更新的问题
被Commonsware 标记为不能setForeground在ImageView上使用方法并由他回答:
这是一个文档错误.setForeground()存在于API Level 1的FrameLayout上; 它仅适用于API级别23的View.
在我的情况下,fork中的检查没有检测到文档错误,但在集成项目中检测到它,我不明白,无论如何,这回答了我的初始问题.
但这不是同一种情况,这个代码已经扩展了Framelayout并且使用了这个方法,它不是一个ImageView,所以我想这个方法已经在API 23中删除了.它现在没有出现在引用中.
更新:我为FrameLayout 添加差异报告:
新问题
所以我的问题改变了如何在以前的版本中使用该方法,为什么有选择地出现检查错误?
如果我添加冗余强制转换,则错误消息将消失:
((FrameLayout) layout).getForeground()
Run Code Online (Sandbox Code Playgroud)
但它正在访问View现在不支持的方法.
因此,我认为针对以前的API会删除错误消息,但我希望使用此Framelayout方法的解决方案或线索来解决我的问题:
ANDROID_COMPILE_SDK_VERSION=23
ANDROID_BUILD_TOOLS_VERSION=23.0.1
ANDROID_DEFAULT_MIN_SDK_VERSION=19
ANDROID_DEFAULT_TARGET_SDK_VERSION=23
Run Code Online (Sandbox Code Playgroud)
解决方法
简而言之,作为一种解决方法,我将类型FrameLayout直接更改为,因此关于API和转换的警告已经消失,我测试它适用于Android 4.4.4设备和调试,我认为它会崩溃,无论如何它只用于一个动画,我将来会修复/更换它.
最初的问题
我分叉和定制的谷歌样品Android的托皮卡,和Android工作室表明,我不理解使用方法所需的API级别的错误getForeground()之类的View在android.view包:
调用需要API级别23(当前最小值为19):android.view.View#getForeground
该引用标记了API级别1中添加的方法,而documentation(Control+Q)显示了相同的内容:
但是Calling new methods on older versions inspection显示需要API级别23的错误: …
java android android-studio android-api-levels android-framelayout
我在动作栏中有一个菜单,我通过以下方式创建:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 98,Menu.NONE,R.string.filter).setIcon(R.drawable.ic_filter_list_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, 99,Menu.NONE,R.string.add).setIcon(R.drawable.ic_add_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Run Code Online (Sandbox Code Playgroud)
和menu_main.xml看起来像:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:icon="@drawable/ic_settings_white_48dp"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
在Espresso中测试时,我想点击"添加"图标(menuId 99).我试过了
@Test
public void testAdd() {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withText(R.string.add)).perform(click());
}
Run Code Online (Sandbox Code Playgroud)
但是这会因NoMatchingViewException而失败.(设置项,直接在xml中定义,我可以使用相同的代码单击.)
这是针对targetSdkVersion 23和AppCompatActivity的.工具栏的相关行是:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
if( getSupportActionBar() != null ) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Run Code Online (Sandbox Code Playgroud)
和tool_bar.xml看起来像:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/ColorPrimary"
android:elevation="4dp"
tools:ignore="UnusedAttribute">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud) 在我写这个问题之前,我已经搜索了相同的问题,他们确实导出了许可证,因为仍然使用alpha版本的约束布局.但是现在android已经发布了稳定版的约束布局.我尝试了很多设置,但仍然失败了..
我的最新消息 .travis.yml
language: android
jdk: oraclejdk8
android:
components:
- platform-tools
- tools # to get the new `repository-11.xml`
- tools # see https://github.com/travis-ci/travis-ci/issues/6040#issuecomment-219367943)
- build-tools-25.0.0
- android-25
# Additional components
- extra-google-google_play_services
- extra-google-m2repository
- extra-android-m2repository
licenses:
- 'android-sdk-preview-license-52d11cd2'
- 'android-sdk-license-.+'
- 'google-gdk-license-.+'
script:
- ./gradlew clean build
Run Code Online (Sandbox Code Playgroud)
这是我的 build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.package.my"
minSdkVersion 16
targetSdkVersion 25
versionCode 6
versionName "1.3.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled …Run Code Online (Sandbox Code Playgroud) android gradle android-sdk-tools travis-ci android-constraintlayout
如果我在我的本地模拟器上运行仪器测试,它们完美地运行10次,但是当我尝试在Travis CI中的AVD上运行相同的测试时,我随机获得
FAILED java.lang.RuntimeException: Could not launch intent Intent { } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was xxxxxxx and now the last time the queue went idle was: …
我正在尝试为我正在进行的项目设置CI,我想知道我们是否真的需要提交gradlew和/或gradle.bat文件才能使其正常工作.
有没有解决方法,或提交这些文件是唯一的方法?
有没有办法列出包的全名,所以我可以安装它.如果我这样做sdkmanager --list,我会得到以下样本:
system-images;a...ult;armeabi-v7a | 4 | ARM EABI v7a System Image
system-images;a...-10;default;x86 | 4 | Intel x86 Atom System Image
system-images;a...pis;armeabi-v7a | 5 | Google APIs ARM EABI v7a Syste...
system-images;a...google_apis;x86 | 5 | Google APIs Intel x86 Atom Sys...
system-images;a...ult;armeabi-v7a | 2 | ARM EABI v7a System Image
system-images;a...ult;armeabi-v7a | 4 | ARM EABI v7a System Image
system-images;a...15;default;mips | 1 | MIPS System Image
system-images;a...-15;default;x86 | 4 | Intel x86 Atom System Image
system-images;a...pis;armeabi-v7a | 5 | …Run Code Online (Sandbox Code Playgroud) 在探索测试时,据我所知,我遇到了ActivityTestRule和IntentTestRule,这是因为IntentTestRule是ActivityTestRule的扩展,并在Espresso Intents中使用。
但从根本上讲,使用这些测试规则的真正目的是什么。