Properties-File won't get recognised! - java

So, I just wanted to use properties-files again, but currently I am just not able to load them! I've already wasted 1h of work just to get this working, but somehow I couldnt. My problem is similar to this one, but Java just doesn't get the file!
Here's my code:
package fast.ProfileManager;
import java.io.FileInputStream;
import java.util.Properties;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
public class PMMain extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String defaultProfileProperties = "defaultProfile.properties";
Properties properties = new Properties();
properties.load(new FileInputStream(defaultProfileProperties));
...
I've already tried to put an "/" infront of the filename, but it didnt work either.
Here's my Project-Directory:
I'm getting an IOException on the line "properties.load ... "

Check out this introduction to using/accessing properties files in Android.
Based on that link, put the properties file in the /assets folder and use the following code:
// Read from the /assets directory
try {
InputStream inputStream = assetManager.open("defaultProfile.properties"");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println("The properties are now loaded");
System.out.println("properties: " + properties);
} catch (IOException e) {
System.err.println("Failed to open microlog property file");
e.printStackTrace();
}

Related

Android Jsoup Parsing URL for all Body Text

Situation: I have been attempting to parse a URL and retrieve the information between the body tags and setting it in the Android Text View.
Problem: Something is wrong and/or missing..
Code:
package jsouptutorial.androidbegin.com.jsouptutorial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textOut = (TextView)findViewById(R.id.rootTxtView);
//------------------Something went wrong here-------------------------------
Document doc;
try {
//doc = Jsoup.connect("https://stackoverflow.com/questions/45311629/android-jsoup-parsing-url-for-all-body-text").get();
doc = Jsoup.parse(new File("https://stackoverflow.com/questions/45311629/android-jsoup-parsing-url-for-all-body-text"), "UTF-8");
Elements desc = doc.select("a.body");
textOut.setText((CharSequence) desc); //Setting textView to a String
} catch (IOException e) {
e.printStackTrace();
}
//--------------------------------------------------------------------
}
}
You have a couple of problems here:
First you are trying to create a File object from a URL, this will throw an IOException. You instead want to use the JSoup method to retrieve the document from the URL
Document doc = Jsoup.connect("https://stackoverflow.com/questions/45311629/android-jsoup-parsing-url-for-all-body-text").get();
The next problem is your elements selection doc.select("a.body"). This is trying to select all anchor tags <a> with a class of body - and there is none. To get the body just use doc.body()
Also as mentioned by cricket_007 you are attempting a network request from the Main thread so it will throw a NetworkOnMainThreadException the easiest way around this will be to run it in an AsyncTask, see this question for details.

Why Java Files.createFile with permissions doesn't work correctly?

I need to ceate file and set permissions(-rwxrw-r) to it, the permission of parent dir is (drwxrwxr--). The problem is that the write permission is
not set in created files. The user that ran this application is the owner of the parent dir.
Below is my test class that present the same problem. When I run this program, the permissions of generated file is (-rwxr--r--) though the class set permissions (-rwxrw-rw-). Why the write permission is not set
and I don't see any exception?
Any idea?
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class TestPermission{
static String parentDir = "/tmp/test/";
static Set<PosixFilePermission> defaultPosixPermissions = null;
static {
defaultPosixPermissions = new HashSet<>();
defaultPosixPermissions.add(PosixFilePermission.OWNER_READ);
defaultPosixPermissions.add(PosixFilePermission.OWNER_WRITE);
defaultPosixPermissions.add(PosixFilePermission.OWNER_EXECUTE);
defaultPosixPermissions.add(PosixFilePermission.GROUP_READ);
defaultPosixPermissions.add(PosixFilePermission.GROUP_WRITE);
//Others have read permission so that ftp user who doesn't belong to the group can fetch the file
defaultPosixPermissions.add(PosixFilePermission.OTHERS_READ);
defaultPosixPermissions.add(PosixFilePermission.OTHERS_WRITE);
}
public static void createFileWithPermission(String fileName) throws IOException{
// File parentFolder = new File(parentDir);
// PosixFileAttributes attrs = Files.readAttributes(parentFolder.toPath(), PosixFileAttributes.class);
// System.out.format("parentfolder permissions: %s %s %s%n",
// attrs.owner().getName(),
// attrs.group().getName(),
// PosixFilePermissions.toString(attrs.permissions()));
// FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(attrs.permissions());
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(defaultPosixPermissions);
File file = new File(fileName);
Files.createFile(file.toPath(), attr);
}
public static void main(String[] args) throws IOException{
String fileName = parentDir + "testPermission_" + System.currentTimeMillis();
createFileWithPermission(fileName);
}
}
I believe the catch here is
The check for the existence of the file and the creation of the new
file if it does not exist are a single operation that is atomic with
respect to all other filesystem activities that might affect the
directory.
as mentioned in Class Files
This might be because of the OS operations that happen after a file is being created. The following modification in code should get things work fine:
File file = new File(fileName);
Files.createFile(file.toPath(), attr);
Files.setPosixFilePermissions(file.toPath(), defaultPosixPermissions); //Assure the permissions again after the file is created
It turns out that the reason is that my os has a umask as 0027(u=rwx,g=rx,o=) which means application has no way to
set permission for others group.
Files.createFile(file.toPath(), attr);
in the above line instead of using Files.createFile
use file.createNewFile() and if it returns true then the file is created.

Error :- Activity Main can't be resolved or is not a field

I searched this error and tried all available solutions but still getting this error. I am new to android so please help to rectify and remove this error.
I am fully aware that this error is somewhat associated with .R file of android.
I am using eclipse and getting 2 error
1- "activity_main cannot be resolved or is not a field"
2- List is a raw type. Reference to generic type.
Following is MainActivity code. This code is written to calculate nearest located Latlong distance..
package com.example.latlong;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import android.R;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class MainActivity extends Activity {
Double latToCompare=0.0, longToCompare=0.0;
float shortestDistance = 0.0f;
Boolean isDistanceComparedFirstTime = true;
Boolean isFirstRowInCSV1 = true, isFirstRowInCSV2 = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); /////// Error1 Line
createDirIfNotExists("/windows/Pictures/LatLongFiles");
String csvFilename1 = Environment.getExternalStorageDirectory()+"/windows/Pictures/LatLongFiles/Data.csv";
String csvFilename2 = Environment.getExternalStorageDirectory()+"/windows/Pictures/LatLongFiles/SourceFile.csv";
String csvFilename3 = Environment.getExternalStorageDirectory()+"/windows/Pictures/LatLongFiles/OutputFile.csv";
CSVReader csvReader1 = null;
CSVReader csvReader2 = null;
CSVWriter csvWriter3 = null;
List csvFile2List = null; ///////////// Error2 Line
I know this error is not new to you android guys but i tried everything and still stuck on this error.. Thanks in advance..
Removed import android.R; And clean and rebuild your project
import your_application_package_name.R;
Error 1 indicates that the build system cannot find a layout file called "activity_main.xml". Verify that you have such a layout file.
Error 2 is simply a warning that informs you that you are using a raw type rather than a generic type. Add a type parameter that indicates the type of objects that are to reside in your list, e.g.:
List<YourTypeHere> csvFile2List = null;
Delete import.android.R; and in place of it, add import com.example.latlong.R; if com.example.latlong is your package name.
Or if this is not your package name, see your manifest file for the package name. And add import YourPackageName.R; instead of import.android.R; where YourPackageName is the name of your package.

getting error in CODEC_ID_MPEG1VIDEO and PIX_FMT_YUV420P

I am trying to create a code which will convert images into video. I have surfed a lot and find some method to do it using javacv. now i am getting variable not found error in these lines.
recorder.setCodecID( CODEC_ID_MPEG1VIDEO);
recorder.setPixelFormat( PIX_FMT_YUV420P);
I have imported all the jar files into my library as well but still getting this error. Some quick help would be appreciated. I have gone through the below link for the importing the javacv into my workspace.
How to Import JavaCV libraries to Android Project
package com.example.photoaday;
import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvShowImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvWaitKey;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import com.googlecode.javacv.FFmpegFrameRecorder;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Recordvideo extends Activity {
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
opencv_core.IplImage img = cvLoadImage("/sdcard/folder/img1.jpg");
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/sdcard/folder/test.mpeg",200,150);
try {
recorder.setCodecID( CODEC_ID_MPEG1VIDEO);
recorder.setFrameRate(30);
recorder.setPixelFormat( PIX_FMT_YUV420P);
recorder.start();
for (int i=0;i<100;i++)
{
recorder.record(img);
}
recorder.stop();
}
catch (Exception e){
e.printStackTrace();
}
}
}
I assume that your compiler gives error as follows
error: ‘CODEC_ID_MPEG1VIDEO’ was not declared in this scope
error: ‘PIX_FMT_YUV420P’ was not declared in this scope
then solution for error is
change : ‘CODEC_ID_MPEG1VIDEO’ to 'AV_CODEC_ID_MPEG1VIDEO'
change : ‘PIX_FMT_YUV420P’ to 'AV_PIX_FMT_YUV420P'

OpenERP Android App connection?

i am new to android environment i have installed eclipse and android developer tool.
my openerp application is running and i coded in "MainActivity.java" in "On create" method that is,
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import org.apache.xmlrpc.client.*;
import org.apache.xmlrpc.client.XmlRpcClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OpenErpConnect connection=null;
try {
URL loginUrl=new URL("http","192.xxx.x.xx",xxxx,"/xmlrpc/common");
XmlRpcClient client=new XmlRpcClient();
Integer id=(Integer)client.call("login","new_db","admin","123456");
connection=new OpenErpConnect(server,port,db,user,pass,id);
}
catch ( XMLRPCException e) {
Log.d(CONNECTOR_NAME,e.toString());
}
catch ( MalformedURLException e) {
Log.d(CONNECTOR_NAME,e.toString());
}
catch ( ClassCastException e) {
Log.d(CONNECTOR_NAME,e.toString());
}
return connection;
}
i am getting error in line "Integer id=(Integer)client.call("login","new_db","admin","123456");"
Errror is,
The type org.apache.xmlrpc.common.XmlRpcController cannot be resolved. It is indirectly referenced from required .class files
What wrong i am doing i want to get result in android app after connecting from openERP
Hopes for your suggestion
Thanks

Categories

Resources