小编Gor*_*dak的帖子

Android:通过自动化测试在 /data/local/tmp 中创建文件

目标: 在设备上进行自动化测试期间截取屏幕截图。测试完成后,使用 adb 拉取屏幕截图文件。

语境:

我目前正在尝试编写自动化测试来拍摄设备屏幕的快照。使用UiDevice进行导航,我想在测试过程中截取屏幕截图。UiDevice 有一个方法takeScreenshot,当我想要拍摄快照时我会调用该方法。

经过一番调查,我意识到负责将图像写入文件UiAutomatorBridge 的类捕获了 Exception :

java.io.FileNotFoundException:/data/local/tmp/screenshots/screen2.png:打开失败:EACCES(权限被拒绝)

我使用adb创建了该文件并为所有用户设置了所有权限。

adb shell touch /data/local/tmp/screenshots/screen1.png
adb shell chmod 777 /data/local/tmp/screenshots
Run Code Online (Sandbox Code Playgroud)

完成后,我可以使用以下命令截取屏幕截图:

@Test
public void takeSnapShot() {
    String filename = "/data/local/tmp/screenshots/screen1.png";
    File file = new File(filename);
    assertEquals(true, mDevice.takeScreenshot(file));
}
Run Code Online (Sandbox Code Playgroud)

问题 :

我希望能够在测试执行时直接创建文件,而不需要使用 adb。我尝试使用createNewFile直接从 Java 创建文件。

try {
     file.createNewFile();
} catch (IOException e) {
     e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个 IOException

java.io.IOException:打开失败:EACCES(权限被拒绝)

有人知道发生了什么事吗?我对 Linux 还很陌生,所以不要犹豫提出建议,即使它对你来说似乎很明显;)

我应该将其发布在超级用户上吗?

编辑

目录/data具有这些权限

drwxrwx--x …
Run Code Online (Sandbox Code Playgroud)

linux android file-permissions android-uiautomator

5
推荐指数
1
解决办法
6230
查看次数

GridView中的Facebook原生广告:MediaView显示灰色矩形

我将Facebook原生广告整合到GridView中.目前,我展示了测试广告.除播放视频的播放器外,它的工作正常.

MediaView播放视频就好了,如果用户不与GridView的交互.滚动GridView时,当广告重新出现在屏幕上时,视频会暂停并恢复.

向上和向下滚动网格几次后,MediaView不再显示视频,只显示一个灰色矩形.

出于好奇,当MediaView为灰色时,我试图在我的设备上运行Ui Automatic Viewer.我注意到一些有趣但我无法理解的东西.

在View层次结构中,我可以看到带有一些子FrameLayout的GridView(适配器给出的Views的容器).这包括原生广告和其他观看次数.

但是当MediaView为灰色时,其FrameLayout 不会出现在View层次结构中!但它在屏幕上呈现得很好!

我对所见所闻感到非常困惑.

此外,当我将这些广告集成到RecyclerView中时,我没有遇到这个问题(或者至少没有注意到它).

我们来谈谈代码吧.我有一个引用指向Facebook原生广告视图.

建议欢迎:)

以下是为GridView提供视图的适配器的代码:

public class AdapterGridGallery extends BaseAdapter implements AdListener {

    private static int POSITION_AD = 4;
    private List<QuizzModel> listQuizzes;

    int heightViews;
    FragmentGallery fragmentGallery;

    View facebookAdView;
    private NativeAd facebookNativeAd;
    private boolean nativeAdSet = false;

    public AdapterGridGallery(FragmentGallery fragment, int height) {
        heightViews = height;
        fragmentGallery = fragment;
        facebookNativeAd = new NativeAd(fragment.getContext(), "my_tag");
        facebookNativeAd.setAdListener(this);
        facebookNativeAd.loadAd();
    }

    public void updateData(List<QuizzModel> list) { …
Run Code Online (Sandbox Code Playgroud)

android gridview facebook-audience-network

5
推荐指数
1
解决办法
1049
查看次数

OkHttp3缓存似乎未使用Retrofit 2进行检查

我正在尝试使用Retrofit(2.1.0)和OkHttp(3.3.1)设置HTTP缓存.我见过很多与此主题相关的帖子,但没有一个有用.

我写了一些单元测试来查看缓存是如何工作的.它工作正常,但一旦集成在我的应用程序中,魔术就结束了.我将首先向您展示我的实施,然后解释我的一些调查.

首先,这是我的Retrofit实例化:

    OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);

    OkHttpClient client = httpBuilder
            .addNetworkInterceptor(INTERCEPTOR_RESPONSE_SET_CACHE)
            .addNetworkInterceptor(INTERCEPTOR_REQUEST_ADD_CHECKSUM)
            .addInterceptor(loggingInterceptor)
            .cache(cacheHttpClient).build();

    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .baseUrl(BASE_URL)
            .build();
Run Code Online (Sandbox Code Playgroud)

这是拦截器添加标头来设置缓存控件:

    private final Interceptor INTERCEPTOR_RESPONSE_SET_CACHE = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());
            response = response.newBuilder()
                    .header("Cache-Control", "max-age=600") //+ Integer.toString(3600 * 5)
                    .build();
            return response;
        }
    };
Run Code Online (Sandbox Code Playgroud)

最后一个拦截器添加了2个URL参数:

 private static final Interceptor INTERCEPTOR_REQUEST_ADD_CHECKSUM = new Interceptor() {
        @Override
        public Response intercept(Interceptor.Chain …
Run Code Online (Sandbox Code Playgroud)

android caching retrofit2 okhttp3

5
推荐指数
1
解决办法
909
查看次数