I am getting a File Not Found exception when I try to access any files using Android Studio. I am able to open a text file with AssetManager but I need to open a p12 file for oAuth Authentication. The code I'm using is taken from https://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_accounts
It seems that files can't be accessed this way in Android Studio? What is the alternative? I am trying to display events from a public calendar so I'm not even really sure I need oAuth (I didn't for a web app).
GoogleCredential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(PlusScopes.PLUS_ME))
.setServiceAccountPrivateKeyFromP12File(new File(KEY))
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
Log.d("FUCK", "cant open");
e.printStackTrace();
}
new File(KEY) creates a file object pointing to the path KEY. That file doesn't exist, so you have problems. If your p12 file is in assets, you'll need to copy it to your phone's file system first and pass in the file, rather than the asset name.
Related
I get in one api a certificate digital with this value so:
response api imagen
I need convert this value to a physical file on Android device any folder how extension .p12
for after upload from the app.
finally read the file with next function FileInputStream:
try {
KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream(filepath), etPassword.getText().toString().toCharArray());
} catch (Exception e) {
}
any help thank you very much
I need to access files on a local sever, and obtain their path for use in a File object.
I don't think I need to use HttpURLConnection for this purpose, do I?
Also, File file = new File(IPAddress) doesn't work.
Where IPAddress looks something like smb://192.168.1.xxx.
Will file:///192.168.1.xxx work for IPAddress?
I'm not able to find a solution on the internet where both accessing a local server and listing its files is achieved.
So, how do I get file paths from a local server for performing File operations?
Edit 1:
By local server I mean a computer on my network on which I have a shared folder.
And I'm building an app that can access that folder and contents in it and do something with them.
I am facing problems fetching file paths to that shared folder content.
Thanks everyone who helped...
My answer might help someone.
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", username, password);
// username and password which you use for logging into your Windows PC
SmbFile network;
try {
network = new SmbFile("smb://servername or IPAddress", auth);
for (SmbFile node : network.listFiles()) {
// network path is now contained in 'node'
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SmbException e) {
e.printStackTrace();
}
You'll have to add<uses-permission android:name="android.permission.INTERNET" />in your AndroidManifest.xml before the <application> tagandActivityCompat.requestPermissions(this /*context*/, new String[]{Manifest.permission.INTERNET}, MY_PERMISSIONS); in your Activity.java for getting access to Internet on API target 23 (Android 6.0) and above.
Download and include in your project jcifs-x.x.xx.jar for SmbFile and NtlmPasswordAuthentication Class, from here.
I am currently having problem create PDFRenderer object. I have a pdf file inside the assets folder and using getAssets().openfd to get the file descriptor.
My code creating the object is:
try {
_fileDescriptor = activity.getAssets().openFd(assetFile).getParcelFileDescriptor();
_pdfRender = new PdfRenderer(_fileDescriptor);
} catch (IOException ex) {
ex.printStackTrace();
}
The error saying : java.io.IOException: not create document. The fileDescriptor isn't null and the assetFile has a correct file path.
I am using the correct android api 21. I tried using lower version android but force close so I think not the phone problem
First: see my question Reading XML online and Storing It (Using Java). Read the approved answers and the comments underneath that answer.
So, what my question here is: even though I've run through the process described in the linked question, and the .xml file saves to the /res/values folder in my Android App, its not showing up at all - not when I'm running the app, nor after I close the app.
Does anyone have any ideas on how to fix this so that when I generate the file, it will be available right away, even while the app is running, to read and use?
just use this code,
FileOutputStream fOut = null;
try {
fOut = this.openFileOutput("your xml file name.xml", MODE_PRIVATE);
try {
InputStream is = new FileInputStream("your source file");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fOut.write(buffer);
} catch(Exception e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
and you can see your xml file at the data/data/packagename/file folder Thnx. Hope this will help you.
I'm not 100% sure if you're running the XML parsing in Java or actually in your Android app.
If you're running in Java, be aware that your project structure isn't live in the emulator - the .apk was packaged up and installed before running. You need to use adb to push files into the emulator (or your Android device) before your app can see the file.
If you're accessing the file in the app:
If you use file access methods such as openFileOutput() it will show up in the private directory on the device, which would be /data/data//files/
However, if you're using "new File(" rather than "context.openFileOutput" then the file is wherever you put it.
My app has .txt files in subdirectories in the assets folder. It reads those .txt files and puts them in a textview. It's working great and no problems.
Should I be concerned about the files in the assets folder getting deleted by the user or missing. If this ever could happen, my app would get an error because the file would not be there when it tried to read it into the stream.
Is there a need for me to check the existence of an asset file before I read it or does the asset manager take care of it all? I also was wondering if there's a chance that a user would or could delete and asset file.
Like I say, everything works fine without me inserting code to check for file existence. I just wondered if people use the .exists() statement every time they go to read in a stream from assets.
You may be concerned that the file have been removed and the apk resigned
You can check using:
Arrays.asList(getResources().getAssets().list("")).contains("myFile")
if you really want to check for the file existence:
AssetManager mg = getResources().getAssets();
InputStream is = null;
try {
is = mg.open(pathInAssets);
//File exists so do something with it
} catch (IOException ex) {
//file does not exist
} finally {
if (is != null) {
is.close();
}
}
If your file is located in assets/folder/file.ext, then pathInAssets would be
"folder/file.ext"
Ideally after apk is built, nobody can remove any assets from it, but if someone decompiled it and recompiles than it may be possible.
Though for other scenarios also when an asset is not present in apk at Runtime, we can check the existence of asset.
In our app, we have a provision to build app using gradle, ant and eclipse, and for each build mechanism some of our assets file are bundled in apk and some are not, so to identify if any asset file is present in current build apk at runtime,
we do this as follows:
private boolean isAssetExists(String pathInAssetsDir){
AssetManager assetManager = AppContext.get().getResources().getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open(pathInAssetsDir);
if(null != inputStream ) {
return true;
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
Yes and No.
A normal user would not be able to delete them, but a user on a rooted phone who doesn't know what they're doing… that's a different situation.
If you ask me, the extra code is not needed. Also if you try and open a file that doesn't exist, you will get an exception thrown at some point, catch that and display a dialog if you really want to.
AssetManager am = getAssets();
try {
List<String> mapList = Arrays.asList(am.list("path/in/assets/folder"));
if (mapList.contains("file_to_check")) {
Log.e("ERROR", "exists");
} else {
Log.e("ERROR", "not exists");
}
} catch ( IOException ex){
ex.printStackTrace();
}
Convert to function or method can be easy ;)
I think you should be OK. From having a root around in my phone I can't see any way of deleting the assests without deleting the app as it all seems to be wrapped up in the .apk file. You can do it but I think you need to be rooted or use adb.
I would personally surround any reading/writing with a try/catch block anyway, just to be safe.