FileNotFoundException when I want to copy a file - android

I've wrote method below to copy my backup file to external storage
public Boolean Backup() {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sd = Environment.getExternalStoragePublicDirectory("");
File data = Environment.getDataDirectory();
String dbPath = "//data//" + "com.example.sqlitetest"
+ "//databases//" + "TestDB";
// Backup file name
Calendar calendar = Calendar.getInstance();
String backupName = calendar.get(Calendar.YEAR) + "-"
+ (calendar.get(Calendar.MONTH) + 1) + "-"
+ calendar.get(Calendar.DAY_OF_MONTH) + "-"
+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + ":"
+ calendar.get(Calendar.SECOND);
String backupPath = "//BackupFiles";
File db = new File(data, dbPath);
File backup = new File(sd + backupPath, backupName);
if (!backup.exists())
backup.mkdirs();
FileChannel src = new FileInputStream(db).getChannel();
FileChannel dest = new FileOutputStream(backup).getChannel();
dest.transferFrom(src, 0, src.size());
src.close();
dest.close();
return true;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
I've added permissions to write and read external storage. The destination folder is created (sd + backupPath) but files no and throws FileNotFoundException! How can I solve this problem?
Thanks in advance

Try:
String dbPath = context.getApplicationInfo().dataDir + File.separator
+ "databases" + File.separator + "TestDB";
And instead of using //, try File.separator.

Your dbPath variable is already a full absolute path name, so can't be used in combination with a File object referring to your data directory, but only on its own. And you got that full path name by making poor assumptions anyway.
What you should do is obtain a File representing the data directory as you are, and use it in combination with a String containing just a filename (within that) and not an absolute directory path.
If you do decide you want directories under your data directory, you can do that, but will have to make sure they exist or create them.

Related

How to link AIML files to Android Studio

Our team is creating a chatbot application this week. We finished coding the AIML files as well as the main codes in Android Studio. The only problem we have right now is the link between these two.
I've already placed the Ab.jar in the libs folder. Also, I've placed the AIML files in the assets folder.
Assets folder
The codes I think are relevant to linking are the following (from ChatActivity.class):
//checking SD card availability
boolean a = isSDCARDAvailable();
//receiving the assets from the app directory
AssetManager assets = getResources().getAssets();
File seedletDir = new File(Environment.getExternalStorageDirectory().toString() + "/bots/seedlet");
boolean b = seedletDir.mkdirs();
if (seedletDir.exists()) {
//Reading the file
try {
for (String dir : assets.list("seedlet")) {
File subdir = new File(seedletDir.getPath() + "/" + dir);
boolean subdir_check = subdir.mkdirs();
for (String file : assets.list("seedlet/" + dir)) {
File f = new File(seedletDir.getPath() + "/" + dir + "/" + file);
if (f.exists()) {
continue;
}
InputStream in = null;
OutputStream out = null;
in = assets.open("seedlet/" + dir + "/" + file);
out = new FileOutputStream(seedletDir.getPath() + "/" + dir + "/" + file);
//copy file from assets to the mobile's SD card or any secondary memory
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//get the working directory
MagicStrings.root_path = Environment.getExternalStorageDirectory().toString() + "/seedlet";
System.out.println("Working Directory = " + MagicStrings.root_path);
AIMLProcessor.extension = new PCAIMLProcessorExtension();
//Assign the AIML files to bot for processing
bot = new Bot("seedlet", MagicStrings.root_path, "chat");
chat = new Chat(bot);
String[] args = null;
mainFunction(args);
}
When I ran the application and started to chat with the bot, the bot incorrectly replies "I have no answer for that"
Chat
How can I solve this problem?
Add these two permissions to manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

Directory: assets/tessdata

I've downloaded an OCR text recognizer from github.
My problem is: I want to launch my app without being online, but everytime I install the apk on my phone, it starts downloading the english language and the tesseract OCR engine.
I've found an online guide which says I have to create a folder in the assets folder called "tessdata" and put the eng.traineddata and the osd.traineddata in this folder.
I've tried but the download process still starts when I install the app for the first time.
What can I do to make this app completely offline?
First, in your project directory in computer (YourProjectDirectory\app\src\main) create assets folder, int this folder create another tessdata folder. In tessdata folder put your .traineddata files, these will be transferred in your phone when your project starts running. You can download .traineddata files for your language HERE.
For transferring .traineddata files into phone I use this code:
public class TessOCR {
public static final String PACKAGE_NAME = "com.example.dainius.ocr";
public static final String DATA_PATH = Environment
.getExternalStorageDirectory().toString() + "/AndroidOCR/";
public static final String lang = "eng";
private static final String TAG = "TESSERACT";
private AssetManager assetManager;
private TessBaseAPI mTess;
public TessOCR(AssetManager assetManager) {
Log.i(TAG, DATA_PATH);
this.assetManager = assetManager;
String[] paths = new String[] { DATA_PATH, DATA_PATH + "tessdata/" };
for (String path : paths) {
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.v(TAG, "ERROR: Creation of directory " + path + " on sdcard failed");
return;
} else {
Log.v(TAG, "Created directory " + path + " on sdcard");
}
}
}
if (!(new File(DATA_PATH + "tessdata/" + lang + ".traineddata")).exists()) {
try {
InputStream in = assetManager.open("tessdata/" + lang + ".traineddata");
OutputStream out = new FileOutputStream(new File(DATA_PATH + "tessdata/", lang + ".traineddata"));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.v(TAG, "Copied " + lang + " traineddata");
} catch (IOException e) {
Log.e(TAG, "Was unable to copy " + lang + " traineddata " + e.toString());
}
}
mTess = new TessBaseAPI();
mTess.setDebug(true);
mTess.init(DATA_PATH, lang);
}
public String getResults(Bitmap bitmap)
{
mTess.setImage(bitmap);
String result = mTess.getUTF8Text();
return result;
}
public void onDestroy() {
if (mTess != null)
mTess.end();
}
}
This code checks whether in your phone exists file with directory /AndroidOCR/tessdata/eng.traineddata and if not, creates one and puts .traineddata file here.
For this to occur, in your OnCreate you will have to create AssetManager, which will let you to access that .traineddata file you placed in your computer in your project.
So in your OnCreate in MainActivity:
AssetManager assetManager = getAssets();
TessOCR tess = new TessOCR(assetManager);
Also, to allow your Android project write data into your phone in AndroidManifest.xml file you need to add permision line:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This is the method I use personally and it works without any errors. If you have any, search in google for answers and if you still can't find an answer, post in comments.

Android - Cannot share vCard from internal cache

I am using a ShareActionProvider to share a vcf file I created.
If I store the file in the external cache, I have absolutely no problems sharing the file, but if I store it in the internal cache, every app I try to share the vCard with says the file is corrupted or unsupported.
I read the file after creating it, and in both cases they are exactly the same.
This code works:
File dir = new File(getExternalCacheDir() + "/contact");
dir.mkdirs();
vcfFile = new File(dir, name.replace(' ', '+') + ".vcf");
However, if I use getCacheDir() instead, I get the problem.
Here's the code for creating the file:
FileWriter fw;
try {
fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:2.1\r\n");
fw.write("N:" + codedName + "\r\n");
fw.write("FN:" + name + "\r\n");
fw.write("ORG:" + org + "\r\n");
fw.write("TITLE:" + position + "\r\n");
fw.write("TEL;PREF;WORK;VOICE;ENCODING=QUOTED-PRINTABLE:" + phone + "\r\n");
fw.write("TEL;PREF;WORK;FAX;ENCODING=QUOTED-PRINTABLE:" + fax + "\r\n");
fw.write("ADR;WORK;;ENCODING=QUOTED-PRINTABLE:" + codedAddr + "\r\n");
fw.write("EMAIL;INTERNET:" + email + "\r\n");
fw.write("URL;WORK:" + website + "\r\n");
fw.write("PHOTO;TYPE=JPEG;ENCODING=BASE64:" + codedImage + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
And here's the code for the ShareActionProvider:
provider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
if (provider != null) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/vcard");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(vcfFile));
provider.setShareIntent(intent);
}
Any ideas of what I'm doing wrong?
Any ideas of what I'm doing wrong?
every app I try to share the vCard with says the file is corrupted or unsupported.
According with Using the Internal Storage
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)...
For this reason, it is advisable to use External Storage
Manifest
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Code
```
public void sharePublicContact(View view){
String name = "Mickey Mouse";
String org = "Disney Corp.";
String note = "";
File dir = new File(getExternalCacheDir() + "/contact");
dir.mkdirs();
File vcfFile = new File(dir, name.replace(' ', '+') + ".vcf");
FileWriter fw;
try {
fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:3.0\r\n");
fw.write("FN:" + name + "\r\n");
fw.write("ORG:" + org + "\r\n");
fw.write("NOTE:" + note + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/vcard");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(vcfFile));
startActivity(sendIntent);
}
```

Writing a zip to internal storage -- file doesn't exist after download completion

I used a tutorial to download a zip into a subdirectory of my application's internal storage. I wrote the zip to /data/data/my.package.name/files/mySubDirectory/the.zip.
But, when I check to see whether the zip exists, it doesn't:
String fileDirectory = this.getFilesDir().getAbsolutePath() + "/mySubDirectory/the.zip";
File file = new File(fileDirectory);
if(file.exists()) {
Log.e(this.class.getName(), "file exists");
} else {
Log.e(this.class.getName(), "file doesn't exist");
}
I verified that fileDirectory is the same path as the File outFile for the FileOutputStream.
What could be the problem?
Try getting your file path as below :
String fileDirectory=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "data" + File.separator+ getActivity().getPackageName()+ File.separator +"mySubDirectory"+File.separator+"the.zip";
Using this SO question, I created a subdirectory using this example:
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 to write into the file
The problem is that I didn't expect the subdirectory to be prepended with "app_", so I was looking for the zip in the wrong place.
Try using getFilesDir() + "/" subdirectory + "/" "the.zip"
Without the getabsolutepath().
That is what I used could be the issue.
OK maybe you problem is with permissions do you see the file in the DDMS under data/data/package/files ? Check the permissions for the files
Here is my code
String path = getFilesDir() + "/"
+ subDirName + "/";
File file = new File(path);
file.mkdirs();
setReadable(file);
I use the following to make the file readable
#TargetApi(9)
private void setReadable(File file) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
try {
Runtime.getRuntime().exec(
"chmod 777 " + file.getCanonicalPath());
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
file.setReadable(true, false);
}
}
}

android programming question

dy_path = Environment.getExternalStorageDirectory() + "\5.jpg";
Instead of that i want how to give dynamic path automatically picture saved based on current time.
I am new to Android. Plz answer my question
You could concatenate the path with
DateFormat.getDateInstance().format(new Date());
That is, use something like
String time = DateFormat.getDateInstance().format(new Date());
dy_path = Environment.getExternalStorageDirectory() + "\\" + time + "\\5.jpg";
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/your folder name" + File.separator);
dir.mkdirs();
String pic = CommonMethod.getRandomString(30);
File file = new File(dir, String.valueOf(pic + ".jpg"));
picturePath = picturePath + String.valueOf(pic) + ".jpg";

Categories

Resources