Is there any way to install a vCard using android app, as soon as it starts for the first time.
Although for running any block of code for the first time, I can use these lines ...
if (isFirstTime()) {
//First time code
}
and
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return ranBefore;
}
but how will I be able to install a vCard from app.
Note: Although I have the vCard already made, and can be put in the raw directory.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard");
startActivity(intent);
Edit
Copy vcard to sdcard
private void copyAssets() { AssetManager assetManager = getAssets();
String[] files = null;
try { files = assetManager.list(""); } catch (IOException e)
{ Log.e("tag", "Failed to get asset file list.", e);
} for(String filename : files)
{
InputStream in = null; OutputStream out = null;
try { in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile); copyFile(in, out); } catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e); }
finally {
if (in != null) { try { in.close();
} catch (IOException e) { // NOOP }
} if (out != null) {
try { out.close(); } catch (IOException e) { // NOOP }
}
}
}
} private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read);
} }
Then type the location where I have written storage_path
Related
I am receiving a stream from the server. That stream represents a PDF, and I KNOW it is a pdf file. I am receiving it, and storing in the phone this way:
ResponseBody body=response.body();
File dir = new File(Environment.getExternalStorageDirectory() + “/myApp/“+variable+"/“+anotherVariable);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, objetoFichero.get("nombre").getAsString());
try {
file.createNewFile();
FileOutputStream fos=new FileOutputStream(file);
InputStream is = body.byteStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len = is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
fos.flush();
fos.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
This way, the file is created, but using a file browser I try to open the pdf, and it opens, but it is blank.
Any idea about what I am doing wrong?
Thank you.
Assuming you have the document (the pdf) as a byteArray (documentBytes)
I would:
createUri(this, new File(getCacheDir(), "pdf/whateverNameYouWant.pdf"), documentBytes)
public static Uri createUri(#NonNull final Context context, final File name, #NonNull final byte[] data) throws IOException {
final File parent = name.getParentFile();
if (!parent.exists()) {
FileUtils.mkdirs(parent, null);
}
final OutputStream out = new FileOutputStream(name);
try {
out.write(data);
} finally {
StreamUtils.closeSilently(out);
}
return createUri(context, name);
}
public static void mkdirs(final File dir) {
if (!dir.mkdirs()) {
Log.w("File", "Failed to mkdirs: " + dir);
}
}
public static void closeSilently(#Nullable final Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (final Throwable ignore) {
}
}
}
You can then show the document with the uri received by createUri()
Please help me out I am not getting the default ringtone file path.
Can any body tell how to get access to the default ringtone in Android. Here is my code for doing that thing. I have commented the path that I gave directly to asset manager to open the file and read it.
public void copyAssets() {
AssetManager assetManager = this.getAssets();
// String FileName="//media/internal/audio/media/";
File io=getFilesDir();
String[] files = null;
try {
files = assetManager.list("");
// files=assetManager.list(FileName);
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for (String filename : files) {
InputStream in = null;
OutputStream out = null;
// File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
// File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
// FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual
//
try {
in = assetManager.open("Ringtone");
// File myFolder = new File(Environment.getDataDirectory() + "/myFolder");
File myFolder = this.getDir("myFolder", this.MODE_PRIVATE);
File fileWithinMyDir=new File(myFolder,"Ringtoness");
out = new FileOutputStream(fileWithinMyDir);
copyFile(in, out);
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
}
public void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}`
I am working on an application in which I have a some text in English and Arabic. For the sake of example I can say it as a words meaning application. The word is in English and user will get it's meaning in Arabic.
For Example:
Test اختبار // Test is the word and then there is it's meaning in Arabic
But when I read this local file I don't get Arabic as intended. Instead I get some strange characters. I am making sure that file is UTF-8 encoded and when I read the file I again pass encoding scheme to be UTF-8..but it does not wwork. Code snippet is as follows:
InputStream inputStream = resources.openRawResource(R.raw.textfile);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
try {
String line;
while((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, " ");
if (strings.length < 2) continue;
addWord(strings[0].trim(), strings[1].trim());
}
} finally {
reader.close();
}
Any help is appreciated..Thanks..!!!
I actually built an helper class that handles FileIO (and is completely compatible with Hebrew) so I guess Arabic will be no problem:
/***
*
* #author Android Joker ©
* Do NOT copy without confirmation!
* Thanks!
*
*/
public class FileMethods {
private Boolean isOk;
private Context mContext;
private String fileName;
public FileMethods(Context c, String FILENAME) {
this.isOk = true;
this.mContext = c;
this.fileName = FILENAME;
}
public void reWrite(Object DATA) {
//For deleting the content of the file and then writing
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write(DATA.toString().getBytes());
fos.close();
Log.i("File Writing ("+this.fileName+")", "Success!");
isOk = true;
}
catch (IOException e) {
e.printStackTrace();
Log.e("File Writing ("+this.fileName+")", "Failed!");
isOk = false;
}
}
public void Write(Object DATA) {
//For keeping the previous contents and continue writing
String data = Read("") + DATA.toString() + "\n";
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
Log.i("File Writing ("+this.fileName+")", "Success!");
isOk = true;
}
catch (IOException e) {
e.printStackTrace();
Log.e("File Writing ("+this.fileName+")", "Failed!");
isOk = false;
}
}
public void Clear() {
//For deleting all the file contents
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write("".getBytes());
fos.close();
Log.i("Cleared"+"("+this.fileName+")", "Success!");
isOk = true;
}
catch (IOException e) {
e.printStackTrace();
Log.e("Cleared"+"("+this.fileName+")", "Failed!");
isOk = false;
}
}
public String Read(String inCaseOfFailure) {
//For reading (If reading failed for any reason, inCaseOfFailure will be written)
String info = "";
try {
FileInputStream fis = mContext.openFileInput(this.fileName);
byte[] dataArray = new byte[fis.available()];
if (dataArray.length>0) {
while(fis.read(dataArray)!=-1)
{
info = new String(dataArray);
}
fis.close();
Log.i("File Reading ("+this.fileName+")","Success!");
isOk = true;
}
else {
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
fos.write(inCaseOfFailure.getBytes());
fos.close();
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Success!");
isOk = true;
}
catch (Exception e) {
e.printStackTrace();
isOk = false;
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "MOVING ON");
}
}
}
catch (FileNotFoundException e) {
try {
FileOutputStream fos = mContext.openFileOutput(this.fileName, Context.MODE_PRIVATE);
if (inCaseOfFailure != null) {
fos.write(inCaseOfFailure.getBytes());
fos.close();
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Success!");
isOk = true;
}
else {
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
isOk = false;
}
}
catch (IOException e1) {
e.printStackTrace();
Log.e("File Writing In Case Of Failure ("+this.fileName+")", "Failed!");
isOk = false;
}
}
catch (IOException e) {
e.printStackTrace();
Log.e("File Reading ("+this.fileName+")", "Failed!");
isOk = false;
}
return info;
}
public Boolean GetIsOK() {
//Method that checks whether the FileIO was successfully running or not
Boolean temp = isOk;
isOk = true;
return temp;
}
}
Each instance of the class handles another file (FILENAME).
Hope this helps!
I am working on Dropbox. I see the documentation. This is my code to display list:
Entry entryCheck = mApi.metadata("/", 100, null, true, null);
Log.i("Item Name", entryCheck.fileName());
Log.i("Is Folder", String.valueOf(entryCheck.isDir));
I got all list from dropbox but my question is that
Here entryCheck.isDir always give me true value if it is file or directory so how i can know which is file or which one is directory?
How i downloaded that files.
I tried with this but it is not working:
private boolean downloadDropboxFile(String dbPath, File localFile,
DropboxAPI<?> api) throws IOException {
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
if (!localFile.exists()) {
localFile.createNewFile(); // otherwise dropbox client will fail
// silently
}
DropboxInputStream fin = mApi.getFileStream("dropbox", dbPath);
br = new BufferedInputStream(fin);
bw = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break;
}
bw.write(buffer, 0, read);
}
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// in finally block:
if (bw != null) {
bw.close();
}
if (br != null) {
br.close();
}
}
return true;
}
This will work
String inPath ="mnt/sdcard/"+filename;
File file=new File(inPath);
try {
mFos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
mErrorMsg = "Couldn't create a local file to store the image";
return false;
}
mApi.getFile("/"+filename, null, mFos, null);
This downloads the file and store it in the sdcard location inPath.
You have to do it in a new thread,not in main thread.Use AsyncTask.
http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html
this link explains why..
Am really frustated now..I want to download a file from Dropbox and save that file into SDCARD..and I got the code as:
private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
if (!localFile.exists()) {
localFile.createNewFile(); //otherwise dropbox client will fail silently
}
FileDownload fd = api.getFileStream("dropbox", dbPath, null);
**br = new BufferedInputStream(fd.is);**
bw = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break;
}
bw.write(buffer, 0, read);
}
} finally {
//in finally block:
if (bw != null) {
bw.close();
}
if (br != null) {
br.close();
}
}
return true;
}
Here I am getting an error on br=new BufferedInputStream line..Pls help
I found the way:
File file= new File("/sdcard/New_csv_file.csv");
OutputStream out= null;
boolean result=false;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
DropboxFileInfo info = mApi.getFile("/photos/New_csv_file.csv", null, out, null);
Log.i("DbExampleLog", "The file's rev is: " + info.getMetadata().rev);
Intent JumpToParseCSV=new Intent(context,ParseCSV.class);
JumpToParseCSV.putExtra("FileName", file.getAbsolutePath());
Log.i("path", "FileName"+ file.getAbsolutePath());
((Activity) context).finish();
context.startActivity(JumpToParseCSV);
result=true;
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while downloading.");
file.delete();
result=false;
}
return result;
Thanks all....