How to get file jpg properties in Android Studio - java

Firstly I get an ArrayList using the method getFilePaths.
method { List out all images from SD card.}
How to continue?
At phone, when you see details over image, you can see:
tittle, hour, width, height, orientation, fileSize, path...
I want get all attributes/details/properties of a file jpg and save them in variables.
I tried do this: Properties Class Java but I think that's not the right way

You can retrieve Properties from File like below code add your file into below code and get Properties object
Properties prop = new Properties();
// load a properties file
prop.load(input);
private void retrievePropertiesFromFile(){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/CHETAN");
String fname = "mytext.txt";
File myFile = new File (myDir, fname);
InputStream input = null;
try {
input = new FileInputStream(myFile);
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
Log.i(getClass().getSimpleName(),prop.getProperty("text"));
Log.i(getClass().getSimpleName(),prop.getProperty("textstyle"));
Log.i(getClass().getSimpleName(),prop.getProperty("typeface"));
Log.i(getClass().getSimpleName(),prop.getProperty("typeface"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Ok, I using this simple code and it show null in property text:
File imageJPG = new File("/storage/emulated/0/WhatsApp/Media/WhatsApp Images","6gMRtQyY.jpg");
if(imageJPG.isFile()){
System.out.println("its file");
System.out.println("image: "+imageJPG);
}else{
System.out.println("no");
}
InputStream input = null;
try {
input = new FileInputStream(imageJPG);
Properties prop = new Properties();
prop.load(input);
// get the property value and print it out
System.out.println("property text: "+prop.getProperty("text"));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Console:
I/System.out: its file
I/System.out: image: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/6gMRtQyY.jpg
I/System.out: property text: null

Related

Loading Java Properties Files by Profile

I want to load Properties Files in Java code.
But I use profile to config -Dspring.profiles.active=local or dev...
How to load properties files by profile Something like this:
classpath:${spring.profiles.active}/test.properties
How to do that in Java code ?
I did as below, but get null.
Properties prop = new Properties();
InputStream iStream = Helper.class.getClassLoader().getResourceAsStream("test.properties");
try {
prop.load(iStream);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
try {
iStream.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
This is some working code for us:
String activeProfile = System.getProperty("spring.profiles.active");
InputStream workSpacesFIS = this.getClass().getClassLoader()
.getResourceAsStream(activeProfile + "/customers.txt");
if (workSpacesFIS != null) { ...
Loading Java Properties Files by Profile
public Properties getProp() throws IOException {
final Properties prop = new Properties();
prop.load(TestService.class.getResourceAsStream("/application.properties"));
String activeProfile = prop.getProperty("spring.profiles.active");
prop.load(TestService.class.getResourceAsStream("/application-"+activeProfile+".properties"));
return prop;
}

FileInputStream fails on properties file

This properties file in PROJECT/resources/properties.properties can be read and show its content with:
public void showFileContent(String fileName){
File file = new File (fileName);
FileInputStream input = null;
if(file.exists()){
int content;
try {
input = new FileInputStream(fileName);
while ((content = input.read()) != -1) {
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
}
But it fails with a null pointer exception at properties.load with that code
public Properties getProperties(String fileName, Properties properties){
File file = new File (fileName);
InputStream input = null;
if(file.exists()){
try {
input = new FileInputStream(fileName);
properties.load(input);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
return properties;
}
even when input is set to
input = this.getClass().getClassLoader().getResourceAsStream(fileName)
anyone knows why that can be for a properties text file at the same path for both methods ?
Since the first code snippet works, it seems properties is passed as null to the getProperties() method, resulting in NullPointerException.
Ideally, we shouldn't be passing the properties at all. We just need to create a new object and return it.

Don't find my file.properties

I try a lot of thinks to find the fail but i don't know how I can do it. my code is:
//DominioLlamadaRedSys.java
Properties d = new Properties();
InputStream entrada = null;
try {
entrada = new FileInputStream("prop/datosApp.properties");
d.load(entrada);
System.out.println(d.getProperty("TXD.endPointUrl"));
} catch (IOException ex) {
System.out.println("ERROR: "+ ex.getMessage());
} finally {
if (entrada != null) {
try {
entrada.close();
} catch (IOException e) {
}
}
}
I call the file inside a class in "com.rsi.secpay.dominio" and this always catch the same exception (don't find the file), I had try to quit "prop/" (just "datosApp.properties" ) with properties files like this:
If your prop package is in your classpath, you can get the stream using the classloader:
InputStream is = DominioLlamadaRedSys.class.getResourceAsStream("/prop/datosApp.properties");

Java: Ideal file format to store a hashmap in?

I need to save an arraylist of hashmaps to an external file. I can use any format expect for a text file, because the program is set to ignore text files (specially, anything with a .txt extension). The hashmaps are pretty straightforward, just words with counts of those words. What is the ideal file format to store this in?
You could use java.util.Properties.
Properties properties = new Properties();
properties.putAll(yourMap); // You could also just use Properties in first place.
try (OutputStream output = new FileOutputStream("/foo.properties")) {
properties.store(output, null);
}
You can read it later by
Properties properties = new Properties();
try (InputStream input = new FileInputStream("/foo.properties")) {
properties.load(input);
}
// ... (Properties implements Map, you could just treat it like a Map)
See also:
Java Tutorials - Essential Classes - Properties
You could use serialization:
ObjectOutputStream stream = null;
try
{
File f = new File(filename);
stream = new ObjectOutputStream(new FileOutputStream(f));
stream.writeObject(your_arraylist);
}
catch (IOException e)
{
// Handle error
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e) {}
}
}
And read it in using:
ObjectInputStream stream = null;
try
{
stream = new ObjectInputStream(new FileInputStream(f));
your_arrayList = (your_arrayList type here)stream.readObject();
}
catch (Throwable t)
{
// Handle error
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e) {}
}
}

How to overwrite a file if it already exists in dropbox?

I am uploading a file in dropbox by this method:
public void upload() {
FileInputStream inputStream = null;
try {
File file = new File(Environment.getExternalStorageDirectory()
.toString() + "/write.txt");
inputStream = new FileInputStream(file);
Entry newEntry = mDBApi.putFile("/write.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
Log.e("DbExampleLog", "User has unlinked.");
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
} catch (FileNotFoundException e) {
Log.e("DbExampleLog", "File not found.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
but when already this file exists in the folder then the file get renamed to write(1).txt
but I want that if the file already exists in the dropbox share folder then it will be replaced. What should I do now?
You can use mDBApi.putFileOverwrite instead of mDBApi.putFile

Categories

Resources