uiautomator takescreenshot not work for no reason - android

I tried to take a screenshot in the internal storage during automation testing,but when run the test,it didn't show any error,but when I check the file,the screenshot didn't take successfully,I am so confused right now,please help me~
Here is part of my code:
I have my permission in my AndroidManifest.xml already
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>
screenshot
UiDevice mDevice =UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.pressHome();
mDevice.takeScreenshot(new File("/sdcard/Pictures/test.png"));
sleep(3000);
I am sure the direction is not wrong cause I tried the same code before,it worked normally,but after few days I tired it again and it failed without showing any error.

Can you try
mDevice.takeScreenshot(new File("/sdcard/Download/test.png"));
sleep(3000);

Related

Android Webview gives net::ERR_CACHE_MISS message

I built a web app and wants to create an android app that has a webview that shows my web app. After following the instructions from Google Developer to create an app, I successfully installed it on my phone with Android 5.1.1.
However, when I run the app for the first time, the webview shows the message:
Web page not available
The Web page at [Lorem Ipsum URL] could not be loaded as:
net::ERR_CACHE_MISS
I solved the problem by changing my AndroidManifest.xml.
old : <uses-permission android:name="android.permission.internet"/>
new: <uses-permission android:name="android.permission.INTERNET"/>
Answers assembled! I wanted to just combine all the answers into one comprehensive one.
1. Check if <uses-permission android:name="android.permission.INTERNET" /> is present in manifest.xml. Make sure that it is nested under <manifest> and not <application>. Thanks to sajid45 and Liyanis Velazquez
2. Ensure that you are using <uses-permission android:name="android.permission.INTERNET"/> instead of the deprecated <uses-permission android:name="android.permission.internet"/>. Much thanks to alan_shi and creos.
3. If minimum version is below KK, check that you have
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWeb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
or
if (Build.VERSION.SDK_INT >= 19) {
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
because proper webview is only added in KK (SDK 19). Thanks to Devavrata, Mike ChanSeong Kim and Liyanis Velazquez
4. Ensure that you don't have webView.getSettings().setBlockNetworkLoads (false);. Thanks to TechNikh for pointing this out.
5. If all else fails, make sure that your Android Studio, Android SDK and the emulator image (if you are using one) is updated. And if you are still meeting the problem, just open a new question and make a comment below to your URL.
For anything related to the internet, your app must have the internet permission in ManifestFile. I solved this issue by adding permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
I tried above solution, but the following code help me to close this issue.
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWeb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
Use
if (Build.VERSION.SDK_INT >= 19) {
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
It should solve the error.
Also make sure your code doesn't have true for setBlockNetworkLoads
webView.getSettings().setBlockNetworkLoads (false);
I ran to a similar problem and that was just because of the extra spaces:
<uses-permission android:name="android.permission.INTERNET "/>
which when removed works fine:
<uses-permission android:name="android.permission.INTERNET"/>

android get number of camera issue - always return max 2

I am developing an app which is using 3 cameras. I have a device which have 3 cameras but when i call Camera.getNumberOfCameras() it always return 2 instead of returning 3. I dont know where the problem is. Can any one let me know if I am missing something.
Permission in manifest
<uses-feature android:name="android.hardware.camera">
<uses-permission android:name="android.permission.CAMERA" />
I found that it is showing 1 front and 1 back camera only. Please help

import android.hardware.Camera does have open() method

Android Studio 0.4.6
Hello,
I am trying to use the camera and I have set the permissions like this:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera"/>
I have imported the correct package:
import android.hardware.Camera;
And I try and open the camera like this:
Camera camera = Camera. there is no open method;
All I get are the following:
Parameters
Class
Size
I have follow the developer documentation, I can't see I have done anything wrong.
many thanks for any suggestions,
Just try to paste camera = Camera.open(); or type it in manually. Sometimes the "clever" thing glitches.
By the way, did the tool suggest to import android.hardware.Camera; or you typed it manually? I mean, is it an Android project? (Typically you start with the automatically generated hello world example and then add extra functionality to it, if you did something else, it's easier to re-do it from the very beginning.)
For anyone in the future facing the same issue
import android.hardware.*;

android camera (froyo) doesnt include static method open()

I must be the first to face this problem because I can't find even a single thread about it.
Today I wanted to start on the camera aspect of my application needs.
I read up some documentation
my manifest looks like this:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
I wrote this in the manifest ABOVE <application> and underneath <manifest>
What im doing is; I have created a new class.
using eclipse as my IDE.
I then declare a field:
Camera _camera;
In the constructor(just to test)
I tried to do:
_camera = Camera.open();
I got an error.
I use my real phone to test the app, because I have no webcam or anytihng for the simulator to use. And the simulator gives me a memory error when I tell it to have a camera.
Anyway, upon finding out why I can't use Camera.open (I included the package: android.graphics.Camera;) Because that is what eclipse included for me when i used to organize imports function.
I looked into the android.jar that eclipse attached for me. Contained in a folder thingy called Android 2.2 -> android.jar
I searched for android.graphics and took a peek in the content of Camera.class
It turns out that my Camera class only has these methods:
Camera()
applyToCanvas()
dotWithNormal()
finalize()
getMatrix()
restore()
rotateX()
rotateY()
rotateZ()
save()
translate()
I intentionally let the parameters out because they are of no importance.
To get to the actual question: Why?
Why is there no open() method, no release() method? and whatever else im missing.. '
Thanks for reading.
Todays lesson: Don't be a smart **s. I was indeed 950% sure I included that specific package. But it was the wrong package. derp. Thanks fo notifying me. Issue is solved.
You are using the wrong Camera.
android.graphics.Camera - A camera instance can be used to compute 3D transformations and generate a matrix that can be applied, for instance, on a Canvas.
android.hardware.Camera - The Camera class is used to set image capture settings, start/stop preview, snap pictures, and retrieve frames for encoding for video. This class is a client for the Camera service, which manages the actual camera hardware.
You are using a wrong Camera class. Use this one http://developer.android.com/reference/android/hardware/Camera.html
You are using Camera from android.graphics.Camera I suppose you need the one from android.hardware.Camera
(yes im 950% sure I have included the package:
android.graphics.Camera;)
You are looking for android.hardware.Camera.

Passing in false to setBlockNetworkLoads() crashes my android app

What is the requirement to make use of this:
mMessageContentView = (WebView) findViewById(R.id.message_content);
mMessageContentView.getSettings().setBlockNetworkLoads(false);
Everytime I set setBlockNetworkLoads to false. My application crashes! If I set it to true, it works fine but my application can't load images over the network.
Is there something I need to do before hand before I can set it to false?
As correctly pointed out by Gallal - I should of made use of the logs. A quick command on dos:
adb logcat
Showed that my application was missing the internet permission in the manifest file. So adding the following to my android manifest solved my problem:
<uses-permission android:name="android.permission.INTERNET" />

Categories

Resources