Hello Im trying to save the language save by the user in my game
I have made a test to see if the script create the file and read it good.
Im using ini4j to store the value of language in a ini file.
I have no file, on the 1st start, programm create the empty file in the Local directory and fill it with value (language=fr) for test.
On 2nd start, the programm check if the file exist, then open it to read the value
The problem is that the 'lang' variable seems to be empty when it read it by my function sic_Language.setLanguage(String text);
My code
String lang;
Wini ini;
//Read
if (Gdx.files.local("mygame.ini").exists()) {
//Open file
try {
ini = new Wini(Gdx.files.local("mygame.ini").file());
} catch (InvalidFileFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Read values
lang = ini.get("interface", "language");
sic_Language.setLanguage(lang); //Set language
}
//Write
else {
//Create file
try {
Gdx.files.local("mygame.ini").file().createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//Open file
try {
ini = new Wini(Gdx.files.local("mygame.ini").file());
} catch (InvalidFileFormatException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
//Write values
ini.put("interface", "language", "fr");
try {
ini.store();
} catch (IOException e) {
e.printStackTrace();
}
sic_Language.setLanguage("fr"); //Set language
}
Related
If trying to open Password Protected PDF with PdfRenderer API, gives SecurityException and handled accordingly, inside catch block and thenonDestroy basic clean up is done, and comes back to home activity and then navigating a simple non-protected PDF than again the same exception occurs.
Please note, this happens only once any protected file got opened.
Refer below code:
#Override
protected PdfRenderer doInBackground(Uri... uri) {
Uri uriToProcess = uri[0];
try {
contentResolver=getContentResolver();
parcelFileDescriptor = contentResolver.openFileDescriptor(uriToProcess, "r");
if(parcelFileDescriptor!=null && mPdfRenderer==null) {
mPdfRenderer = new PdfRenderer(parcelFileDescriptor);
}
} catch (FileNotFoundException e) {
exceptionMsg="Sorry! No such file or directory found";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ FNFException", e.toString());
} catch (IOException e) {
exceptionMsg="Sorry! Something went wrong with IO";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ IOException", e.toString());
} catch (SecurityException e) {
if (parcelFileDescriptor!=null) {
try {
parcelFileDescriptor.close();
parcelFileDescriptor = null;
contentResolver=null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (mPdfRenderer!=null){
mPdfRenderer.close();
mPdfRenderer=null;
}
exceptionMsg="Password protected file, This can't be opened";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ SecurityException", e.toString());
} catch (Exception e) {
exceptionMsg="Sorry! Something went wrong.";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ EXCEPTION", e.toString());
}
return mPdfRenderer;
}
Any help cordially appreciated.
I had the same problem with my app. The way I solved it was using an https://github.com/TomRoush/PdfBox-Android and load the document and check for password protection. After no InvalidPasswordException has been thrown the file can be safely loaded with the PdfRenderer.
I am trying to store the json data to the file.This is the code for creating directories and file.
public void filecreation() throws IOException {
File mediaStorageDir = new File(getActivity().getFilesDir() + "/" +
"Zerribelum");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("App", "failed to create directory");
// return null;
} else {
Log.d("Apppppp", "create directory");
File textfolder = new File(mediaStorageDir, "Text");
if (!textfolder.exists()) {
textfolder.mkdirs();
File newtext = new File(textfolder, "dummy.txt");
stream = new FileOutputStream(newtext);
bw = new BufferedWriter(new OutputStreamWriter(stream));
}
File imagefolder = new File(mediaStorageDir, "Image");
if (!imagefolder.exists()) {
imagefolder.mkdirs();
}
File videofolder = new File(mediaStorageDir, "Video");
if (!videofolder.exists()) {
videofolder.mkdirs();
}
}
}
}
I want put the json data to the file.
try {
JSONArray mJsonTopicArray = mJsonObject.getJSONArray(AppConstants.APIKeys.TOPICS_LIST);
for (int i = 0; i < mJsonTopicArray.length(); i++) {
Topics mTopics = new Topics();
mTopics.setId(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.ID));
mTopics.setTitle(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS));
mTopics.setImage(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS_IMAGE));
mTopics.setDescription(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.DESCRIPTION));
mTopicsArrayList.add(mTopics);
bw.write(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.DESCRIPTION));
bw.newLine();
}
bw.close();
mCategoryListRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mCategoryListRecyclerView.setHasFixedSize(true);
mCategoryListRecyclerView.setAdapter(new ParallaxRecyclerAdapter(context, HomeFragment.this, mTopicsArrayList));
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
The probelm is if I have'nt add bw.write and bw.close() lines the data will display in recyclerview. But when I add this lines, data is adding to file but not showing in recylcer view. I want both to be done. What will be the problem?
Note: public FileOutputStream stream;
public BufferedWriter bw; Initialised.
Check your stacktrace, your code must be crashing and you arent noticing it because of try catch
Attach a debugger and check the items in the arraylist once the adapter is initialized. If items are present it will work
solved,the issue is because of Nullpointer Exception.So handled using try catch.
try {
bw.write(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS));
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
I am using a MediaPlayer that grabs videos from URLs:
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When I try and set a datasource with a url, that for instance, has been taken down, I get the following error in my logcat:
Couldn't open file on client side; trying server side:
java.io.FileNotFoundException: No content provider:
http://example.com/examplevideo.mp4
However, my catch{} does not trap this in the following code above, which I need it to do so I can display an error message etc...
Is there any way to catch this FileNotFoundException in my code?
Thanks in advance
I don't know how to catch this specific exception. But you can use following to catch any type of exception.
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(this, uri);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
Log.i("your tag",e.toString());
}
and look at the log with the tag you whant
Thanks all for replies.
I forgot to mention that the file is from an external server, but have adopted an approach similar to what Pramod suggested, but altered to suit my needs.
/**
* Checks if an external file on a server exists.
* #param urlString The file URL
* #return true if file exists, false if does not
*/
public boolean checkExternalFileExists(String urlString){
try {
URL url = new URL(urlString);
int response = ((HttpURLConnection)url.openConnection()).getResponseCode();
return response == HttpURLConnection.HTTP_OK;
}
catch (FileNotFoundException e) {
return false;
}catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
Thanks
I am trying to write some json data to the file, below is what i have tried.
JSONObject obj = new JSONObject();
try {
obj.put("name", "mkyong.com");
} catch (JSONException e2) {
e2.printStackTrace();
}
try {
obj.put("age", new Integer(100));
} catch (JSONException e2) {
e2.printStackTrace();
}
ArrayList list = new ArrayList();
list.add("msg 1");
list.add("msg 2");
list.add("msg 3");
try {
obj.put("messages", list);
} catch (JSONException e1) {
e1.printStackTrace();
}
FileOutputStream fs;
String filename = "Sample.json";
try {
fs = openFileOutput(filename, Context.MODE_APPEND);
fs.write(obj.toString().getBytes());
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I dont get any errors though, but the file created is not visible. Should i have to read it to verify its written properly?? It would be of great help if i can see the created file in the src folder since i write many stuff, its easy to validate.
Probably lame, but we can locate the file in the DDMS perspective and pull out tag will will be visible!! From there, we can pull it out and view it from the local computer!!
Try to change
String filename = "Sample.txt";
instead of
String filename = "Sample.json";
Im doing a program where u can draw your signature on the phone. Right now it saves one image but I would like to save more than one image since there are more than one customer that needs to sign their package. very thankful for any kind of help.
public void save() {
File sdImageMainDirectory = new File("/sdcard/mySignatures");
sdImageMainDirectory.mkdirs();
String nameFile = "newpic";
FileOutputStream out = null;
try {
out = new FileOutputStream(sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out=null;
}
Rather obviously, you need to change the filename to something unique for each one. Numbering them sequentially would work. Or you let the user enter a name, and validate it for legality.