Android: Unable to create PDFRenderer - android

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

Related

Use new File() with Android Studio

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.

File path for Sparsitiy Sparksee file/database

I am using Sparksee database, which requires to provide a path for the database file. Comparing it with sqlite, I simply wrote the filename without any path.
try
{ gdb = sparksee.create("filepath", "alias"); }
catch (FileNotFoundException e) {
{ e.printStackTrace(); }
It gives an FileNotFoundException on running.
java.io.FileNotFoundException: File cannot be created
The documentation/API does not specify any special path requirements, it simply says to provide a path for the file. I tried using the default database path :
/data/data/com.example.sparksee/databases/file
But it does not work as well.
What path should be provided in such a scenario?
I used this code to get a file path :
in Activity :
String path = this.getFileDirs().getPath();
in DatabaseCode :
gdb = sparksee.create(path + "/filepath", "alias");
The file was created in the /data/data/package/file/ folder. The application ran perfectly well without any hiccups.

Class Recognition; R.drawable.balloons

Okay, I seem to be having a small issue with R.drawable.balloons. I'm trying to use a template for building a private external storage file that I found on Android Developer, but balloons keeps giving an error (cannot be resolved or is not a field). I was wondering if I could get some help fixing it.
Here's the code section it sits in:
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
/*
Very simple code to copy a picture from the application's
resource into the external file. Note that this code does
no error checking, and assumes the picture is small (does not
try to copy it in chunks). Note that if external storage is
not currently mounted this will silently fail.
*/
// Creates file to stream picture
OutputStream os = new FileOutputStream(file);
// Allows app to accept the picture
InputStream is = getResources().openRawResource(R.drawable.balloons);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
A heads up, in case I get called out for being a copy/paster, this is only supposed to be a template, but I would like to test that it works before I make changes. Sorry.
You need an image named balloons in the res\drawable folder of your project (or any of its variants, such as drawable-hdpi, &c). The R class is autogenerated.
See How do I add R drawable android?

Android and LibGDX : create XML file in local storage

I want to check if in my /assets/game/ directory there is a file level.xml and if it does not exist I want to create it. I use this code:
private static void LoadXML() {
ProgressFileHandle = Gdx.files.local("game/level.xml");
if (!ProgressFileHandle.exists()) {
try {
ProgressFileHandle.file().createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else ProgressFileHandle.file().readString();
}
This code does not work, I get the error:
java.io.IOException: No such file or directory
it works if I change
Gdx.files.local("assets/game/level.xml");
to
Gdx.files.local("level.xml"); But I don't understand why. How can I create this file in a subdirectory (assets/game)?
I'm not clear if you're expecting to read from the standard assets folder that is packaged with your game. You should be using Gdx.files.internal() to read from there in that case, though that assets folder is always going to be read-only on Android.
If you want to create another directory called assets/ in the Libgdx "local storage" (which is equivalent to the Android internal storage) you will need to create the directories before you can create a file in the new directory. Just invoke .mkdirs() on the file handle to make sure all the required parent directories are created.
ProgressFileHandle.file().mkdirs();
ProgressFileHandle.file().createNewFile();

Saving XML files to a Running Android Project

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.

Categories

Resources