I try to write string to file in my class, extends AsyncTask:
protected void onPostExecute(String result) {
super.onPostExecute(result);
DataOutputStream out = null;
try {
out = new DataOutputStream(openFileOutput("1xdf.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i=0; i<20; i++) {
try {
out.writeUTF(Integer.toString(i));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.close();
Log.e(DEBUG_TAG, "6x");
} catch (IOException e) {
e.printStackTrace();
}
}
Where can I finde this file in DDMs? When I try search->file 1xdf.xml, I get
Problems encountered during text search. File
'MainActivity/bin/jarlist.cache' has been skipped, problem while
reading: ('Resource is out of sync with the file system:
'/MainActivity/bin/jarlist.cache'.'). Resource is out of sync with the
file system: '/MainActivity/bin/jarlist.cache'.
When you use context.openFileOutput(), the files are written into the app's data. To check what this path is, you can call getFilesDir().
In the DDMS perspective in Eclipse, select the 'File Explorer' tab and then open the following path:
data/data/com.your.package.name.here/files/1xdf.xml
Where 'com.your.package.name.here' is the name of your project's package / application name. If you want to move the file to your file system, select the 'Pull a file from the device' button on the top right (looks like a floppy disk with a pink arrow pointing to the left) and select the directory on your computer where you want to save the file.
If you want to manually load a file onto your virtual device, use the button to the right of the one above (that looks like a right facing arrow pointing to a phone).
Related
after I run any application on my phone, using free version of AIDE -IDE Android, everytime I view LogCat, I get the same message : " run the app to see the log output ".!
Here is the following screenshot :(https://i.stack.imgur.com/uLORU.png)
Is LogCat free on AIDE-IDE Google play app ?
Thank you for your attention.
It's free as far as i know, but you need root access in order for log to work.
Besides: it doesn't work from time to time with root either.
Another option: use following function to log to local file:
public void appendLog(String text)
// https://stackoverflow.com/a/6209739
{
File logFile = new File("sdcard/log.file");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.flush();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Use it like this:
try{
// your code goes here
}catch (Exception e){
appendLog(e.getMessage());
}
You need to add permission for writing_external_storage in Manifest.
There is no problem with your code. However the problem is with AIDE version your using. Am using the Pro and Logcat is working fine for me
I found this way, as shown below, using Log.getStackTraceString(Exception e), to solve my LogCat View trouble on AIDE-IDE Android. The only remaining question is why there is no display of Log.e (TAG,"Exception ",e) ?
Thank you for your attention.
Code of MainActivity (https://i.stack.imgur.com/Dqfc5.png)
I am trying to download a file using the Box android SDK. The problem seems to be with the destinationFile parameter. The box.com call is checking whether the destinationFile exists - but why? I get java.io.FileNotFoundException.
destinationFile = new File(getFilesDir(), "myfile.crs");
// destinationFile = new File(getFilesDir(),"/");
try {
BoxDownload fileDownload = mFileApi.getDownloadRequest(destinationFile, fileID)
// Optional: Set a listener to track download progress.
.setProgressListener(new ProgressListener() {
#Override
public void onProgressChanged(long numBytes, long totalBytes) {
// Update a progress bar, etc.
}
})
.send();
} catch (BoxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
To do All the checks
Log.i(getClass().getName(),"Does File exists:"+(destinationFile.exists()?"Yes":"No"));
Log.i(getClass().getName(),"Is it A file:"+(destinationFile.isFile()?"Yes":"No"));
Log.i(getClass().getName(),"Is it Writable:"+(destinationFile.canWrite()?"Yes":"No"));
Log.i(getClass().getName(),"Is it A Readable:"+(destinationFile.canRead()?"Yes":"No"));
Log.i(getClass().getName(),"Path:"+destinationFile.getAbsolutePath());
you are most likely to found file does not exists then do this before using it.
if(!destinationFile.exists()){
try {
destinationFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
The answer was to create a new File. Then call .createNewFile() on the File instance. Then call all of the code I posted except to put run it in the background. That's why I was asking here - I was wondering if I was doing something incorrect for Android and I was. Box networking operations need to be done on a thread.
Too bad they never show this for downloading a file. Too bad there's not one example of downloading a file with the Android DSK on the web.
I have an app that can take a text file from the devices internal storage and reads the contents of the file to which it places it into an arraylist for me to compare against some other data.
Some example data in the text file is:
1.10
1.11
1.12
My app displays the last element of the array list, so in this example it would be '1.12' that is displayed.
The issue I am having is that if I change '1.12' to '1.13', it only updates to the new value when I run the app for the second time.
I can't figure out why this is. I have tested to make sure the file does update using adb and thats all updated but it appears as if the app is still reading the contents of the old version of the file rather than the new overwritten file on the first run. My code is below for reading the files contents.
public void readVersionList(){
download = (TextView) findViewById(R.id.download_version_path);
try {
Scanner scan = new Scanner(new File("/sdcard/version-nums.txt"));
ArrayList<String> list = new ArrayList<String>();
while (scan.hasNext()){
list.add(s.next());
}
scan.close();
updateVersionNum = list.get(list.size()-1);
download.setText(updateVersionNum);
Log.i("VERSIONNUM", updateVersionNum);
}catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
I'm using usb-serial-for-android library and I am getting some strange results. After 2.5 hours of continuous communicating with the serial port and reading and writing successfully, I get this exception:
exception in UsbManager.openDevice
android.os.TransactionTooLargeException
at android.os.BinderProxy.transact(Native Method)
at android.hardware.usb.IUsbManager$Stub$Proxy.openDevice(IUsbManager.java:339)
at android.hardware.usb.UsbManager.openDevice(UsbManager.java:255)
at com.hoho.android.usbserial.driver.UsbSerialProber$1.probe(UsbSerialProber.java:63)
at com.hoho.android.usbserial.driver.UsbSerialProber.probeSingleDevice(UsbSerialProber.java:174)
But when i force close my app and then restart it, everything is fine and my app can communicate with the port again.
It maybe useful to mention that before the exception , I get this exception:
java.io.FileNotFoundException: /sdcard/log.txt: open failed: EMFILE (Too many open files)
at libcore.io.IoBridge.open(IoBridge.java:406)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileWriter.<init>(FileWriter.java:58)
at org.example.myapp.util.L.log(L.java:32)
I use class L for logging purposes:
public class L {
public synchronized void log(String message){
File logFile = new File("sdcard/log.txt");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(new Date(DateProvider.getInstance().getCurrentDateAsMillisecs()).toString()+": "+message);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
It seems that some kind of buffer or cache is getting filled and prevents communicating with the device.
How can I get rid of the exception?
Update:
Writing to log file never fails even though I'm getting the exception. It only affects communicating with usb device.
You are opening too many files.
Problems I have found:
- You never close logFile
- You open the file in the log function, a static File would be better and could be initialized in a log_init(); function.
Or, make logFile an object variable and initialize it in the constructor.
I think solving these will solve your problem.
Can you not use getFilesDir() to pass the directory path while creating file
public class L {
public static File logFile = new File("sdcard/log.txt");
public synchronized void log(String message){
....
Everytime you call new File you request a file resource from the system. As you keep doing it the system will complain that you opened too many files. To avoid this, request the file only once.
http://www.isco.com/webproductimages/appBnr/bnr1.jpg
I've used a website to see the metadata of a image. In this website, it shows all info of image. I want to know how to get the "Title" tag of above image in android.
I found here the similiar question for iOS only: How to get image metadata in ios
However I don't know how to get "Meta data" of image on android. ExifInterface only gives some informations. But I'm unable to get "Title" tag with it.
Can you provide any code snippet for get meta data in android for image?
Download metadata extractor from the link given here ...... click to download the library
choose the version 2.5.0.RC-3.zip
Extract the jar Folder
and import jar into libs folder in your poject and then execute the below code
try {
InputStream is = new URL("your image url").openStream();
BufferedInputStream bis = new BufferedInputStream(is);
Metadata metadata = ImageMetadataReader.readMetadata(bis,true);
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}
}
catch (ImageProcessingException e){}
catch (IOException e) {}
If you want to retrieve meta data information about an image ExifInterface is what you are looking for. Here is a quite good example of how this interface is used: http://android-er.blogspot.com/2009/12/read-exif-information-in-jpeg-file.html
But if you want to retrieve information of an online image I'm afraid it's not yet possible.
If you want to retrieve metadata from image in Android project, then you can do that with the help of: https://github.com/drewnoakes/metadata-extractor
Implement this in your gradle using
implementation 'com.drewnoakes:metadata-extractor:2.12.0'
Complete code is as follows
private static String getImageMetaData(Uri image1) {
try {
InputStream inputStream = FILE_CONTEXT.getContentResolver().openInputStream(image1);
try {
image1.getEncodedUserInfo();
Metadata metadata = ImageMetadataReader.readMetadata(inputStream);
for (Directory directory1 : metadata.getDirectories()) {
if (directory1.getName().equals("Exif IFD0")) {
for (Tag tag : directory1.getTags()) {
if (tag.getTagName().equals("Date/Time")) {
return tag.getDescription();
}
}
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}