I would like to upload a string as a text file to skydrive from my android application. Is this possible?
Thanks.
Yes it is possible, you have to create a file from the user Entered String, and then save this .txt file on your Server(skydrive)
Its the code which will create a file from the string.
private String filename = "MySampleFile.txt";
private String filepath = "MyFileStorage";
File myInternalFile;
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory , filename);
FileOutputStream fos = new FileOutputStream(myInternalFile);
fos.write(myInputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
Toast.makeText(context, MySampleFile.txt saved to Internal Storage...", 1000).show;
}
Now save this file in skydrive using skydrive api.
Related
Given this code:
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/";
String file = dir + "info.txt";
File newfile = new File(file);
//Environment.getExternalStorageDirectory().toString()= /storage/emulated/0
String msgData="p1000";
FileOutputStream outputStream;
try {
newfile.createNewFile();
outputStream = openFileOutput(file, Context.MODE_PRIVATE);
outputStream.write(msgData.getBytes());
outputStream.close();
} catch (Exception e) {
e.toString();
//e.printStackTrace();
}
When I open file /storage/emulated/0/Documents/info.text, I find it empty while it should have the string "p1000";
why is that?
note: I do not have external storage (external sd card).
thanks.
Do you have WRITE_EXTERNAL_STORAGE Permission in your Manifest?
Try this: FileOutputStream outputStream = new FileOutputStream(file);
If you wanna use internal storage you should not pass a path pointing at the external storage (roughly sd card). In you try-catch block use this line instead:
outputStream = openFileOutput("info.txt", Context.MODE_PRIVATE);
The file will be saved in the directory returned by Context.getFilesDir().
I am saving my file to app's private internal storage like this then I pass that file into a Uri so I can return it as a result in my other activity
String filename = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(data);
outputStream.close();
Intent intent = new Intent();
intent.setData(Uri.fromFile("")); //pass file here somehow
setResult(Activity.RESULT_OK);
finish();
} catch (IOException e) {
Toast.makeText(CameraActivity.this, "Error creating file " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
How do you get the File from openFileOutput ?
How do you get the File from openFileOutput ?
Use Context.getFilesDir() for getting file path which is created using openFileOutput method:
String rootDir = this.getFilesDir().getAbsolutePath();
File file = new File(rootDir + filename);
.....
intent.setData(Uri.fromFile(file));
I made this solution for the very same problem, I like this one better:
Uri internalFilesUri = Uri.fromFile(getContext().getFilesDir());
Uri fileUri = Uri.withAppendedPath(internalFilesUri , filename);
Where is the android data folder exists ?since i have to save large files and users should not able to see them ,i think to use data folder.But i don't know where is located in android phone.does it is on sdcard or not ?
You should use Internal storage
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.
Sample code:
File file = new File(context.getFilesDir(), filename);
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
catch (IOException e) {
// Error while creating file
}
return file;
}
For more details, please refer here.
Use the below to get the path of of data directory
File f = Environment.getDataDirectory();
System.out.println(f.getAbsolutePath());
I'm a newbie Android developer. I have loaded an image using universal-image-loader and I would like to save it on my sd card. The file is created in the desired directory with the correct filename, but it always has a size of 0. What am I doing wrong?
A relevant snippet follows:
PS: The image already exists on disk, it's not being downloaded from the Internet.
private void saveImage(String imageUrls2, String de) {
String filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
File SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsoluteFile();
String filename = de;
File myDir = new File(SDCardRoot+"/testdir");
Bitmap mSaveBit = imageLoader.getMemoryCache();
File imageFile = null;
try {
//create our directory if it does'nt exist
if (!myDir.exists())
myDir.mkdirs();
File file = new File(myDir, filename);
if (file.exists())
file.delete();
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bos.flush();
bos.close();
} catch (IOException e) {
filepath = null;
e.printStackTrace();
Toast.makeText(getApplicationContext(),
R.string.diskful_error_message, Toast.LENGTH_LONG)
.show();
}
Log.i("filepath:", " " + filepath);
}
Yes, your code creates an file on sdcard_root/testdir/de only, and didn't write anything to it. Is "imageUrls2" the source image file? If yes, you can open that file with BufferedInputStream, read the data from BufferedInputStream, and copy them to output file with bos.write() before bos.flush() and bos.close().
Hope it helps.
save and upload a file from internal memory in android
I had try to save a file into internal memory
String smsXml = "<messages><sms><From>" + address + "</From><Date>" + finalDateString + "</Date><Body>" + msg +"</Body></sms></messages>";
try {
//saving the file as a xml
FileOutputStream fOut = openFileOutput("textMessage.xml",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(smsXml);
osw.flush();
osw.close();
}
catch (Throwable e) {
Log.d("Exception","Exception:"+ e);
}
Now I try to upload the file to server But how can I get file path to upload this.I had successfully uploaded a .mp3 from sd card but how to do that from internal memory.Follow this link for uploading .mp3
final String uploadFilePath = "android/data/data/com.example.sms/files/";
final String uploadFileName = "textMessage.xml";
Use getFileStreamPath(). This will return the file created by openFileOutput method in Files directory.
File file = getFileStreamPath("textMessage.xml");
Then you can get the path String with getPath method:
String path = file.getPath();