My Android app is requesting through a HTTP POST command a download request. This download is a MultiPart session sending JSONObjects and File (Image).
but when I'm trying to retrieve the File from:
ObjectInputStream objIn = new ObjectInputStream(con.getInputStream());
File p = new File(path, o.photo_id+".png");
p.canWrite();
p = (File) objIn.readObject();
My file p is getting a 0 length...
as I'm not familiar with InpuStream/OutputStream I don't know exactly how to write my File physically in memory....and first to retrieve it !
any idea is more than welcome !
Related
Looking for a way to prevent users from opening a text file that i transfer from android to PC
Through DropBox.
is convert the text file to bin file Will do the job ?
can i get any sample code in java (for android) that convert text file to bin file ?
i try this but dont work:
Reading the file:
File queryImg = new File(ImagePath);
int imageLen = (int)queryImg.length();
byte [] imgData = new byte[imageLen];
FileInputStream fis = new FileInputStream(queryImg);
fis.read(imgData);
Writing the file:
FileOutputStream f = new FileOutputStream(new File("/MyPath/xx.bin"));
f.write(imgData);
f.flush();
f.close();
If I correctly understand the question, I'd suggest either changing file extension from .txt to otherwise nonexistent extension of your choice (.gldsft or something like that), encrypting, or not going with text file at all.
I need to download some pdf files into data/data/com.**.* folder.
Those files are application specific and only application should read and display it that's the reason storing on data/data/com.**.* folder.
Please let me know how to download into that folder and open/read it in the application.
I know how to download it into SD card, but I do not have idea to downloading to application specific folder.
Please let me know some code examples to do this and also I need to know the capacity/size of the data/data/com.**.* folder.
As long as you want write your own applications Data folder, you can create a FileOutputStream like this FileOutputStream out = new FileOutputStream("/data/data/com.**.*/somefile"); than use that output stream to save file. Using the same way you can create a FileInputStream and read the file after.
You will get Permission Denied if you try to access another application's data folder.
I am not sure for capacity but you can calculate the size of the data folder using this
File dataFolder = new File("/data/data/com.**.*/");
long size = folderSize(dataFolder);
...
public static long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
lengthlong += folderSize(file);
}
return length;
}
Hi here i am attaching the link of a tutorial explained.
http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
and there are many discussions going on internet that you should root your phone in order to access the data from data/data folder and I am also attaching some links about the discussion, I hope these are also some of the links that are related to your question
where do i find app data in android
How to access data/data folder in Android device?
and as well as some links that makes out the things without rooting your phone i mean
You can get access to /data/data/com*.* without rooting the device
http://denniskubes.com/2012/09/25/read-android-data-folder-without-rooting/
To Write file
FileOutputStream out = new FileOutputStream("/data/data/your_package_name/file_name.xyz");
To Read file
FileInputStream fIn = new FileInputStream(new File("/data/data/your_package_name/file_name.xyz"));
Now you have your input stream , you can convert it in your file according to the file type .
I am giving you example if your file is contain String data the we can do something like below ,
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String mDataRow = "";
String mBuffer = "";
while ((mDataRow = myReader.readLine()) != null) {
mBuffer += mDataRow + "\n";
}
Remember to add write file permission to AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am using this code for creating the new file and write the json object in test.json file but I am unable to create file.Please find mistake and give the best result
One things I have also write the code for permission in manifest file..
Thanks,
String fileName = "test";
File file = new File("/data/data/packagename/test.json");
file.createNewFile();
DataOutputStream fos = new DataOutputStream(openFileOutput(fileName , Context.MODE_PRIVATE));
if(file.exists()){
fos.write(jsString.getBytes());
fos.close();
}
This will not land on C:/ anyway. At best at your SD card or some other folder, and since android is a unix based system. you should be careful on how you use "slashes"
I'm trying to upload a file to an FTP using apache commons ftp.
I am using a code that I have seen on several websites, including stackoverflow
Android FTP Library
The problem is that in the line:
Buffin = new BufferedInputStream (new FileInputStream (file));
I can not put any paths in "file", eclipse does not validate any values or path-
What would have to indicate in "new FileInputStream"?
I do not know I'm doing wrong.
Thank you very much and best regards
You need a File object to pass it to the FileInputStream.
Buffin = new BufferedInputStream(new FileInputStream(new File("/path/to/file"));
And you can't Upload file to FTP because FTP is not a place, it is a protocol.
Create a File object with the path and pass it in.
You can do this:
Buffin = new BufferedInputStream (new FileInputStream (<path to your file>));
Details here:
FileInputStream
hi all:
making a sample application in which we can store data on File and then could Read it.,
m using the following code.:
Code to Write:
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(openFileOutput("finear.fin", 0));
out.write(data); //data is String variable
out.close();
Toast.makeText(getApplicationContext(), "Data has been Saved! ", Toast.LENGTH_LONG).show();
}
Code to Read:
instream = openFileInput("finear.fin");
if(instream!=null)
{
InputStreamReader inputReader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputReader);
String dataRead = buffreader.readLine();
}
Now problem is: The Data is stored in simple Text Form but i want that no one could read my data from that file and it should store the data in some binary form or some symbols type format so that when one opens the file in windows it should not display my text stored in the file or should display some other data.. BUT also that data could be readable in String form when i read. PLease Help
You can use Android's internal storage, which is private to your application:
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
Here you can find the docs: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Encrypt the String before saving it to the stream and the other way to read it. If you want your file's contents to be unreadable, encryption would be a win.