unity web request got error for local files on mobile - unity3d

I load images from my local destination to unity the code is:
Texture2D imgTexture;
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(filePath))
{
yield return req.SendWebRequest();
if (req.isNetworkError)
{
Debug.Log(req.error);
}
else
{
imgTexture = DownloadHandlerTexture.GetContent(req);
}
}
it works on pc but when I run on android emulator I get an error
Cannot connect to destination host
the local file directory comes from simple file browsing plugin so I am thinking directory cannot be wrong.
is it a permission problem how can I fix this?

Related

invalid argument: File not found error when trying to upload a file

I have an E2E test to upload a file to the application which works on my local machine but fails to run on Browserstack. But fails with reason :
invalid argument: File not found : /home/travis/build/xx/xx/e2e/src/xx/testfile
Here is the code
let fileToUpload = 'testfile';
let absolutePath = path.resolve(__dirname, fileToUpload);
await browser.setFileDetector(new remote.FileDetector());
let fileElem = $('input[type="file"]');
await fileElem.sendKeys(absolutePath);
I have the files upload in my code base for travis to pick them.
Any inputs are appreciated.
Thanks
Since the file upload is working from your local machine, you can try using the Local File Detector Option.
driver.setFileDetector(new LocalFileDetector());
driver.get("http://www.fileconvoy.com/");
driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\url.txt");
driver.findElement(By.id("readTermsOfUse")).click();
driver.findElement(By.name("form_upload")).submit();
The above code snippet will upload a file located on the local machine. The same details are available here: https://www.browserstack.com/automate/java#enhancements-uploads-downloads
You can port this in the language of your choice.
((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
driver.get("https://www.monsterindia.com/seeker/registration");
WebElement browser = driver.findElement(By.xpath("//*[#id=\"file-upload\"]"));
//Upload button xpath
browser.sendKeys("add here upload file path");
System.out.println("File upload Successfully");
This will also simple way to upload file. I have worked this code in Chrome browser.
This code worked for me to run the test in BrowserStack. I'm using Java-selenium-Jenkins-BrowserStack
WebElement uploadFile = driver.findElement(By.xpath("//input[#type='file']"));
((RemoteWebElement)uploadFile).setFileDetector(new LocalFileDetector());
upload_file.sendKeys(System.getProperty("user.dir")+"/src/main/java/resources/common/<Name of your file>.csv");

AssetBundle Unity on Server

I would like to ask if anyone create Assetbundle in Unity on cloud? I would like to generate the AssetBundle dynamically on cloud and the client app will download it accordingly.
Could you let me know your idea? Is there any cloud service for hosting Unity ?
The accepted answer is spot on but there are slight changes as Unity5.6 now supports every other feature in the free version too. I've been working on a similar project that required building asset bundles dynamically. I'll post my code snippet for the same so that the process of identifying this gets simpler for everyone else in future.
But before that, there are some limitations for this process that you may need to consider. Building asset bundles dynamically on cloud requires (Command line) batchmode which runs Unity on commandline (Unity should be installed to build bundles). This asset building process works only on Windows and OSX (No Linux). The command to invoke Unity in batch mode is given below and has to be executed from Unity executable's location,
this command creates an empty project,
Unity -batchmode -quit -createProject <path/to/create a project>
After creating a project, you can save a script to build assets in the Assets/Editor folder, I have a written a script to automate the process of building assetbundle for all assets in the Assets/Models folder.
//BuildAssets.cs
using System.Collections;
using System.Collections.Generic;
public class BuildAssets : UnityEngine.MonoBehaviour
{
static void BuildAssetBundle()
{
int i = 0;
string log = "log.txt";
string[] assetN;
int N_Files;
UnityEditor.AssetBundleBuild[] AssetMap = new UnityEditor.AssetBundleBuild[2];
AssetMap[0].assetBundleName = "res";
// Adding to path /Models
string path = UnityEngine.Application.dataPath + "/Models";
//log
System.IO.File.AppendAllText(log, System.DateTime.Now.ToString() + "\n\n");
System.IO.File.AppendAllText(log, path + "\n");
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
System.IO.FileInfo[] files = dir.GetFiles();
// Number of files in "/Models" folder
N_Files = files.Length;
//log
System.IO.File.AppendAllText(log, "Num assets: "+N_Files + " \n");
assetN = new string[N_Files];
foreach (System.IO.FileInfo file in files)
{
if (file.Exists)
{
if (!file.Extension.Equals(".meta"))
{
assetN[i] = "Assets/Resources/" + file.Name;
System.IO.File.AppendAllText(log, assetN[i] + " \n");
i += 1;
}
}
}
AssetMap[0].assetNames = assetN;
UnityEditor.BuildPipeline.BuildAssetBundles("Assets/AssetBundles", AssetMap, UnityEditor.BuildAssetBundleOptions.None, UnityEditor.BuildTarget.Android);
System.IO.File.AppendAllText(log, "\t----X----\n"); //log
}
}
This is the command for building the asset bundle through command line.
Unity -batchmode -quit -projectPath path/to/UnityProjects/Projectname -executeMethod BuildAssets.BuildAssetBundle -logFile <Log file location>
I've tested this and it works for our project.
AssetBundles require unity pro to build. There is a command line batch mode that you can use to build your asset bundles automaticly and host it on virtualy any host (a simple HTTP get).
Remember that you might not need asset bundles (or unity pro) - you can easily download textures and audio from the web using the WWW class. For textures you can use png, jpeg and tiff, for audio wav, ogg (only desktop and webplayer), mp3 (only mobile). Mesh loading should be also possible but that will require additional tools.

Background update/upgrade of custom Android application [not using com.android.vending]

Context:
Earlier this year Facebook updated their android app without using the google play services : http://liliputing.com/2013/03/facebook-pushes-android-update-to-enable-silent-updates-bypassing-the-play-store.html
I would like to try a similar thing with my android app.
(I'm aware of the implications so please don't post regarding the same.)
Description:
I have gone through the the related Android classes Package Manager - Androidxref and resources like: Install apps silently, with granted INSTALL_PACKAGES permission but could not find any non-rooted method.
Ways like:
public static int installAPP(String absolutePath){
int status = -1;
File file = new File(absolutePath);
if(file.exists()){
try {
String command = "adb install -r " + StringUtil.insertEscape(absolutePath);
Process install = Runtime.getRuntime().exec(new String[] { "su", "-c", command });
status = install.waitFor();
} catch (Exception e) {
Log.e(TAG, "Not Installed!");
} finally { return status;}
}
}
works just fine, but then again it's for rooted devices.
Any information/hints for possible ways of doing and and any relevant information/hacks regarding the same is much appreciated.
Thank You.
PS. I am working on a ROM.

Zend_Validate_File_IsImage can't detect PNG

I'm using Zend_Validate_File_IsImage on a file upload, just using this code, essentially:
$adapter = new Zend_File_Transfer_Adapter_Http;
$adapter->addValidator('IsImage', false);
if (!$adapter->receive()) { /* error... */ }
I took the image found here (just happened to have that page open):
http://www.codinghorror.com/blog/images/coding-horror-official-logo-small.png
saved it as horror.png and tried to uploaded it, and it works fine on my dev machine (Mac OSX), but it fails on the production server (Ubuntu) saying:
File 'horror.png' is no image, 'text/plain' detected

"Android version 4.2.2 - Unable to invoke wget to download file or via JAVA Socket."

I am trying to use wget with my Android app, which targets Android v 4.2.2. I granted the app Internet permission thru the AndroidManifest.xml file, but that did not solve the issue. I am still unable to download the file with wget command. I also tried to use a java socket to accomplish the connection to the server and download the file, however, that did not work. It seems that Android version 4.2.2 has restrictions on allowing network access thru Android apps. Could someone please help me identify a work around for getting network access so wget command can work from the Android app? Below is the code I am using to test the command.
private String runShellCommand() {
try {
Process process = Runtime.getRuntime().exec("system/bin/wget -O /data/data/com.shell/filename http://url to the file");
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
int numRead;
char[] buffer = new char[5000];
StringBuffer commandOutput = new StringBuffer();
while ((numRead = bufferedReader.read(buffer)) > 0) {
commandOutput.append(buffer, 0, numRead);
}
bufferedReader.close();
process.waitFor();
return commandOutput.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Your goal seems to be to download a file rather than using wget correctly. To download the file within your app, which is the conservative and portable solution, try using the URL class and the HttpURLConnection class, or other classes available on android and suitable for downloading based on URLs.