I have application that i am testing and i want to push file into my android device (real device)
This is what i have try:
self.driver.push_file('/mnt/sdcard/Pictures/photo.png', r'C:\photo.png')
So this operation is pass and i can see the file on my device but its size is 1kb and when i try to open it i have this message:
Its looks like we dont support this file format
What i am doing wrong ?
Pay attention that using Appium with the Python language, when you call the self.driver.push_file() method, the second parameter is the Contents of file in base64 (and not the path of the file on your machine).
That means that you first have to read the file, convert it to base64 (and decode it using utf-8) and only then pass it to this method:
import base64
...
with open(r'C:\photo.png','rb') as file:
driver.push_file('/mnt/sdcard/Pictures/photo.png',
base64.b64encode(file.read()).decode('utf-8'))
Alternatively, you can simply use the following command (adding just source_path= to your snippet):
self.driver.push_file('/mnt/sdcard/Pictures/photo.png', source_path=r'C:\photo.png')
..since the push_file() method was recently updated to support also source path (see Pull #270):
def push_file(self, destination_path, base64data=None, source_path=None)
I would recommend on the source_path parameter of course :)
Related
How can I save a file locally on an Android device,
using Delphi (XE5, Firemonkey)?
Something as simple as
Memo.Lines.SaveToFile('test.txt')
does not seem to work.
It results in the following error message:
"Cannot create file "/test.txt". Not a directory."
According to the document Creating an Android App, get the documents path like this:
System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim + 'myfile';
Instead of using System.SysUtils.PathDelim, you may use
System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt');
Combine chooses between the windows \ and the Linux /
System.IOUtils must be used in this line instead of setup in the uses clause because there are probably more Tpath initials.
GetHomePath, return a string with the path.
-> 'data/data//files'
You can to use:
Memo.Lines.SaveToFile(format('%s/test.txt', [GetHomePath]));
Or this form
Memo.Lines.SaveToFile(GetHomePath + '/test.txt');
on my device (and presumably all android devices?) GetHomePath incorrectly gives me /storage/emulated/0/... whereas I need /storage/sdcard0/... to get to the storage visible in Windows Explorer via USB.
so the full path to my files may be
'/storage/sdcard0/Android/data/com.embarcadero.(my app name)/files/'
Presumably if you have a plug in SD card this might be sdcard1 or whatever.
You can list the contents of your device storage folder with code like this
P := '/storage/';
if (FindFirst(P + '*', faAnyFile, Sr) = 0) then
repeat
Memo1.Lines.Add(Sr.Name);
until (FindNext(Sr) <> 0);
FindClose(Sr);
On my device this gives me:
sdcard0
usb
emulated
then change S when you want to explore subfolders
Note that the files folder gets emptied each time you recompile and deploy.
I'm trying to save some text on file (test.txt) on my android phone but unsuccessful. This is my code example:
procedure TDM.WriteLog(text:string);
var
myFile : TextFile;
begin
// Try to open the Test.txt file for writing to
AssignFile(myFile, System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt'));
ReWrite(myFile);
// Write a couple of well known words to this file
WriteLn(myFile, text + sLineBreak );
// Close the file
CloseFile(myFile);
end;
When i locate file on my phone i open file but it is empty. What I missed?
When you want to create a text file in Android (it's the same with iOS and macOS of course) you can use the WriteAllText class procedure that belongs to the TFile class. You have to add in the uses clause System.IOUtils.
TFile.WriteAllText(TPath.Combine(TPath.GetDocumentsPath, 'test.txt'), 'content');
When you want to store a text file, or in general data related to your app, it's recommended that you use the GetDocumentsPath. This is a modern way to create a text file but it's not the only one; consider also that you could:
Use a TStringlist and then call SaveToFile('path')
If you are using a memo it has the SaveToFile('path') function
Use the TStreamWriter class (and the StreamReader to read the content)
I am trying to read an image in my C++ code
LOGD("Loading image '%s' ...\n", (*inFile).c_str());;
Mat img = imread(*inFile, CV_LOAD_IMAGE_GRAYSCALE);
CV_Assert(img.data != 0);
and get the following output:
09-25 17:08:24.798: D/IRISREC(12120): Loading image '/data/data/com.example.irisrec/files/input/osoba1.jpg' ...
09-25 17:08:24.798: E/cv::error()(12120): OpenCV Error: Assertion failed (img.data != 0) in int wahet_main(int, char**), file jni/wahet.cpp, line 4208
The file exists. But strange is, that if I try to preview the image using Root File Browser it is just black. I copied the files there manually.
EDIT:
The code works fine under Windows with .png and .jpg format. I am just trying to port an existing C++ project for Iris Recognition to Android.
imread() determines the type of file based on its content not by the file extension. If the header of the file is corrupted, it makes sense that the method fails.
Here are a few things you could try:
Copy those images back to the computer and see if they can be opened by other apps. There's a chance that they are corrupted in the device;
Make sure there is a file at that location and that your user has permission to read it;
Test with types of images (jpg, png, tiff, bmp, ...);
For testing purposes it's always better to be more direct. Get rid of inFile:
Example:
Mat img = imread("/data/data/com.example.irisrec/files/input/osoba1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!img.data) {
// Print error message and quit
}
When debugging, first try to get more data on the problem.
It's an unfortunate design that imread() doesn't provide any error info. The docs just say that it'll fail "because of missing file, improper permissions, unsupported or invalid format".
Use the debugger to step into the code if you can. Can you tell where it fails?
Search for known problems, stackoverflow.com/search?q=imread, e.g. imread not working in OpenCV.
Then generate as many hypotheses as you can. For each one, think of a way to test it. E.g.
The image file is malformed (as #karlphillip offered). -- See if other software can open the file.
The image file is not a supported format. -- Verify the file format on your desktop. Test that desktop OpenCV can read it. Check the docs to verify the image formats that AndroidCV can read.
The image file is not at the expected path. -- Write code to test if there's a file at that path, and verify its length.
The image file does not have read permission. -- Write code to open the file for reading.
A problem with the imread() arguments. -- Try defaulting the second argument.
I was able to solve this issue only by copying the image files in code.I stored them in my asset folder first and copied them to internal storage following this example.
If someone can explain this to me please do this.
It could be a permission issue.You would have to request the permission from Java code in your Activity class like this in Android 6.0 or above. Also make sure that in your AndroidManifest.xml, you have the the following line :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your activity file add this:
if (PermissionUtils.requestPermission(
this,
HOME_SCREEN_ACTIVITY,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
Mat image = Imgcodecs.imread(filePath,Imgcodecs.IMREAD_COLOR);
}
I struggled a long time to find this and I was getting Mat object null for all the time before.
Using Titanium on Android 4+ I want to access a jpeg file which has been taken with the camera. I need to achieve 2 objectives, namely, return the EXIF data and transfer the bytes to an API endpoint. My problem is I'm unable to access the file...
I'm using a 3rd party module to handle the file selection (Multi Image Picker) which returns a list of file locations, using the File Manager app on the emulator (GenyMotion) I can confirm the location on disk is correct. However, the following always returns false...
var file = Ti.Filesystem.getFile('/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg');
Ti.API.info('Do we have a file? ' (file.exists()? 'YES' : 'NO'));
The output for the above would be... Do we have a file? NO
Further reading shows Titanium has 5 predefined folder locations which can be passed into the getFile() method and one possible reason for the above code not working would be it is defaulting to the 'Resouces' folder location? That said all but one folder location is app specific, the exception being externalStorageLocation. Now my understanding of an Android device is that any image taken with the camera will be stored on the internal storage system unless an SD card is present. This is true in my case as the following lists 0 files...
var extDir = Ti.Filesystem.getExternalStorageDirectory();
var dir = Ti.Filesystem.getFile(extDir);
var dir_files = dir.getDirectoryListing();
Ti.API.info('External files... ' + dir_files.length);
The output for the above would be... External files... 0
So am I right in thinking Appcelerator have simply not included the ability to access local storage (outside of any app specific folders) within their API? Or am I missing something and there is in fact another way?
Thanks to #Bharal I was able to find a solution...
By using the Ti.Media.openPhotoGallery() method I was able to identify the correct native path for the image by inspecting the event object returned from the success callback.
The path was missing 'file://' at the beginning, I couldn't be 100% sure but I suspect this forces the getFile() method to use an absolute path and not a relative path from within the Resources folder.
To confirm, the following will return a file object...
var file = Ti.Filesystem.getFile('file://[path]');
Where [path] is the folder location as reported within the File Manager app on the device, for example '/mnt/sdcard/DCIM/Camera/IMG_20140901_083735.jpg'
Yah mon, i dunno.
Here is wat i used when i was doin pictures on my Ti app, but then i got rid of that section because i realised i didn't need to be doin pictures. Pictures mon, dey ain' what you want sometimes, yo?
Ti.Media.openPhotoGallery({ //dissall jus' open up a piccha selectin' ting. ez.
success:function(event){
var image = event.media;
if (event.mediaType==Ti.Media.MEDIA_TYPE_PHOTO){
//so image.nativePath is the path to the image.
// profileImg be jus' some Ti.UI.createImageView ting yo be puttin in yo' page.
//meyybe yo be wantin' alert(image.nativePath); here too, dat be helpin?
profileImg.image = image.nativePath;
}
},
cancel:function(){
//we cancelled out, why we doin' that?
}
});
Now that isn't going to really be helpin' you, but yo can use that to see wat the native path yo piccha be usin' be, and then be seein' if maybe what yo be puttin' in yo code be sam ting.
Jus' wrap the above as an addEventListener("click", function(){ ... } ); on sam ting in yo page, and jus' add sam element to put th' piccha in if yo be wantin' to see the piccha but i be tellin' you picchas mon, sometimes dey ain' worth time.
But meyybe yo wantin' use not an emulator for dis ting, dey can be actin' weird yo should be usin some small phone maybe? Dat way you can be findin' if yo got dem memory leeks and meyybe some memory sprouts, an memory onions too.
How can I save a file locally on an Android device,
using Delphi (XE5, Firemonkey)?
Something as simple as
Memo.Lines.SaveToFile('test.txt')
does not seem to work.
It results in the following error message:
"Cannot create file "/test.txt". Not a directory."
According to the document Creating an Android App, get the documents path like this:
System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim + 'myfile';
Instead of using System.SysUtils.PathDelim, you may use
System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'test.txt');
Combine chooses between the windows \ and the Linux /
System.IOUtils must be used in this line instead of setup in the uses clause because there are probably more Tpath initials.
GetHomePath, return a string with the path.
-> 'data/data//files'
You can to use:
Memo.Lines.SaveToFile(format('%s/test.txt', [GetHomePath]));
Or this form
Memo.Lines.SaveToFile(GetHomePath + '/test.txt');
on my device (and presumably all android devices?) GetHomePath incorrectly gives me /storage/emulated/0/... whereas I need /storage/sdcard0/... to get to the storage visible in Windows Explorer via USB.
so the full path to my files may be
'/storage/sdcard0/Android/data/com.embarcadero.(my app name)/files/'
Presumably if you have a plug in SD card this might be sdcard1 or whatever.
You can list the contents of your device storage folder with code like this
P := '/storage/';
if (FindFirst(P + '*', faAnyFile, Sr) = 0) then
repeat
Memo1.Lines.Add(Sr.Name);
until (FindNext(Sr) <> 0);
FindClose(Sr);
On my device this gives me:
sdcard0
usb
emulated
then change S when you want to explore subfolders
Note that the files folder gets emptied each time you recompile and deploy.