Hi I want to acces my bin file from asset folder and read file size in android.This code work for ZIP file but did not work BIN file.
try {
InputStream is = context.getAssets().open("test.bin");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
} catch (Exception e) { throw new RuntimeException(e); }
size is always 0.How can I get bin file size correctly? Thanks
Related
I am trying to use tesseract-ocr in my android app. When I am trying to init() I get IllegalArgumentException because in this folder there is no 'tessdata' dir! Here is my project structure. project structure
Here I used InputStream and cacheDir:
private String getDirPath() {
File f = new File(getCacheDir()+"/tessdata/");
if (!f.exists()) try {
InputStream is = getAssets().open("tessdata/eng.traineddata");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { Log.e("error", e.toString()); }
Log.i("wtf", f.getPath());
return getCacheDir();
}
To init the Tesseract I have to pass 2 arguments - path to dir which contains directory 'tessdata' and second one is traineddata.
Any ideas?
You can't refer to your app's raw asset files that way. Try using AssetManager instead.
The path to your assets is
Uri path = Uri.parse("file:///android_asset/")
String dataPath = path.toString();
With this code, I am able to get the text file. If I replace text file with docx file it shows data in Encoded form and I need to show the text in proper format. Is it possible with .docx file ? or any other solution for that.
try {
InputStream is=getActivity().getAssets().open("getting_started.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer);
// Finally stick the string into the text view.
tv_help_text.setText(text);
} catch (IOException e) {
throw new RuntimeException(e);
}
I need to read "strings.json" file that lies in the "assets" folder of my Android project. But
File file = new File(filepath);
Logger.e(file.exists() ? "exists" : "doesn't exist");
says that the file doen't exist. I've tried the following variants of the filepath:
strings.json
android_asset/strings.json
/android_asset/strings.json
file:///android_asset/strings.json
What's wrong?
For read files in Assets:
InputStream is = context.getAssets().open("strings.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fileResult = new String(buffer, "UTF-8");
In fileResult you should retrieve the content of your file.
AssetManager manager = getAssets();
try {
InputStream stream = manager.open(string+".xml");
} catch (IOException e) {
e.printStackTrace();
}
I've been looking at this site for the past 3 or so hours. How to copy files from 'assets' folder to sdcard?
This is the best I could come up with because I'm only trying to copy one file at a time.
InputStream in = null;
OutputStream out = null;
public void copyAssets() {
try {
in = getAssets().open("aabbccdd.mp3");
File outFile = new File(root.getAbsolutePath() + "/testf0lder");
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: ", e);
}
}
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);
}
}
I've figured out how to create a file and save a text file. http://eagle.phys.utk.edu/guidry/android/writeSD.html
I would rather save an mp3 file to the sdcard rather than a text file.
When I use this code I provided, I get a text document that same size as the aabbccdd.mp3 file. It does not create a folder and save an .mp3 file. It saves a text document in the root folder. When you open it, I see a whole bunch of chinese letters, but at the top in English I can see the words WireTap. WireTap Pro was the program I used to record the sound so I know the .mp3 is passing through. It's just not creating a folder and then saving a file like the above .edu example.
What should I do?
I think you should do something like that -[Note: this i used for some other formats not mp3 but its works on my app for multiple format so i hope it will work for u too.]
InputStream in = this.getAssets().open("tmp.mp3"); //give path as per ur app
byte[] data = getByteData(in);
Make sure u have the folder already exists on path, if folder is not there it will not save content correctly.
byteArrayToFile(data , "testfolder/tmp.mp3"); //as per ur sdcard path, modify it.
Now the methods ::
1) getByteData from inputstream -
private byte[] getByteData(InputStream is)
{
byte[] buffer= new byte[1024]; /* or some other number */
int numRead;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try{
while((numRead = is.read(buffer)) > 0) {
bytes.write(buffer, 0, numRead);
}
return bytes.toByteArray();
}
catch(Exception e)
{ e.printStackTrace(); }
return new byte[0];
}
2) byteArrayToFile
public void byteArrayToFile(byte[] byteArray, String outFilePath){
FileOutputStream fos;
try {
fos = new FileOutputStream(outFilePath);
fos.write(byteArray);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I am new to Android and Samba. I am trying to use the JCIFS copy. To method to copy a file from a Samba directory to the 'Download' directory under sdcard on an Android 3.1 device. Following is my code:
from = new SmbFile("smb://username:password#a.b.c.d/sandbox/sambatosdcard.txt");
File root = Environment.getExternalStorageDirectory();
File sourceFile = new File(root + "/Download", "SambaCopy.txt");
to = new SmbFile(sourceFile.getAbsolutePath());
from.copyTo(to);
I am getting a MalformedURLException on the 'to' file. Is there a way to get around this problem using the copyTo method, or is there an alternate way to copy a file from the samba folder to the sdcard folder using JCIFS or any other way? Thanks.
The SmbFile's copyTo() method lets you copy files from network to network. To copy files between your local device and the network you need to use streams. E.g.:
try {
SmbFile source =
new SmbFile("smb://username:password#a.b.c.d/sandbox/sambatosdcard.txt");
File destination =
new File(Environment.DIRECTORY_DOWNLOADS, "SambaCopy.txt");
InputStream in = source.getInputStream();
OutputStream out = new FileOutputStream(destination);
// Copy the bits from Instream to Outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Maybe in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}