Get object File from a file in raw folder - android

What i want to do is very simple :
String dataDir = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).applicationInfo.dataDir;
Log.e(TAG , "dataDir = " + dataDir);
File tmpFile = new File(dataDir + "/raw/ocr.jpg");
But i got a file not found exception
i NEED an object file as after i do
photoUriString = MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), tmpFile.getAbsolutePath(), null, null);
(because i want to share the picture on google+, see How to share image in google Plus through an android app?)
So please don't tell me about a reader, a bufferedReader, etc ...
Thanks

InputStream is = getResources().openRawResource(R.raw.ocr.jpg));
you have to get the InputStream through openRawResource, then you can manage it as you need

Related

Not Able to access expansion file content

I am using this code, but I am not able to access the expansion file content, i want to show gif image from expansion file, how can i do?
String packageName = getPackageName();
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + "/Android/obb/" + packageName);
try {
if (expPath.exists()) {
String strMainPath = expPath
+ File.separator
+ "main."
+ getPackageManager().getPackageInfo(
getPackageName(), 0).versionCode + "."
+ packageName + ".obb";
File f = new File(strMainPath);
if (f.exists()) {
Log.e("Path ", "=====>Exists");
} else {
Log.e("Path ", "=====> Not Exists");
}
ZipResourceFile zip = new ZipResourceFile(strMainPath);
InputStream iStream = zip.getInputStream("stage1_popup.gif");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeStream(iStream, null, option);
Glide.with(SampleDownloaderActivity.this).load(bitmap).into(image);
}
} catch (Exception e) {
}
http://prntscr.com/kp25qz
The Play APK expansions files library is completely open source, and you can see the sourcecode for ZipResourceFile here.
It looks like stage1_popup.gif is not in your obb file. To investigate it, why not use adb pull to get the file off your device and see what it actually contains. Or download the source code and attach to your IDE so you can step into the getInputStream() call and see where it is going wrong.
As mentioned in this answer, ZipResourceFile isn't able to deal with too much little files and neither is ZipFile. So try to divide your files in more directories.
Also, it's quite possible that there isn't any file with the name, stage1_popup.gif.
Alternatively, you can get all Entries via zipResourceFile.getAllEntries() and findout if the file exists.

Unable to create file in Android app

I am making a android app in which i am going to use File handling but to do that first i need to create it but by using following code:
File logf = new File("log1.txt");
Boolean bb = logf.exists();
if(!bb)
try {
bb = logf.createNewFile();
} catch (IOException e) {
msg.setText("not able to create file.");
}
'msg' is the TextView object which i am using to display error, and when i run this app.. it goes into IOException e catch.. Please tell me if i am doing something wrong or what?.. besides i am using Android 2.2 for my app.
In app storage, please try this:
File file = new File(getFilesDir(), "file.txt");
Or if you want to append file name, try this:
File file = new File(getFilesDir() + File.separator + "file.txt");
you need to provide absolute path of file like below.
File file = new File(/data/packagename/files/ + "log1.txt");

Unity 3D Android and File

I have a problem with Android and Unity 3D. I have a file read code. When I put my code on the computer, it works. However, my code does not work on Android (Mobile). How can I solve this problem? Thank you.
FileInfo theSourceFile = new FileInfo(filename);
if (System.IO.File.Exists(fname))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
EDIT updated code
string filename = "file.txt";
FileInfo theSourceFile = new FileInfo(filename);
filename = Application.persistentDataPath + "/"+filename;
System.IO.File.WriteAllText(filename,"Test");
if (System.IO.File.Exists(filename))
{
StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(string);
}
You need to change your build settings for android Device.
Change Configuration >> write access to external (sd card).
if not, your app is pointing to internal path and you need root permission in your android device.
You must use Application.persistentDataPath on Android to be able to read anything.
Change that to string filename = Application.persistentDataPath + "/file.txt"; and your code should work fine.
Bear in mind that before the read function can work, you must write to the directory first. So file.txt must exist in Application.persistentDataPath first.
For example
string filename = Application.persistentDataPath + "/file.txt";
System.IO.File.WriteAllText(filename,"Test");
EDIT:
You new code is still not working because you had FileInfo theSourceFile = new FileInfo(filename); before filename = Application.persistentDataPath + "/"+filename;. This means that the file name is still not valid. Pay attention the order your script execute. After switching it, it worked on my Android. Below is the whole code.
string filename = "file.txt";
filename = Application.persistentDataPath + "/" + filename;
System.IO.FileInfo theSourceFile = new System.IO.FileInfo(filename);
System.IO.File.WriteAllText(filename, "Test");
if (System.IO.File.Exists(filename))
{
System.IO.StreamReader reader = theSourceFile.OpenText();
string text = reader.ReadLine();
print(text);
}

unable to open a word file using android app

I have saved a file with .docx extension in my app.the file is saved in the sdcard. The file appears as a word file in my sdcard but I am unable to open it (using polaris or any other default software) and message"unsupported file" appears.
When I save the file with .txt extension, I can open it.
public void Savedoc(View v)
{
String filename = "file" + sn + ".docx";
String filepath = "MyFileStorage";
myExternalFile = new File(getExternalFilesDir(filepath), filename);
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(ly.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
thank you alexandru ...but now i get an error message on running the app stating "The Javadoc for this element could neither be found in the attached source nor the attached Javadoc".pls help...
You'll need to use Apache POI in order to correctly create a .docx file.
I've found this answer with a code snippet:
XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText("LALALALAALALAAAA");
tmpRun.setFontSize(18);
document.write(new FileOutputStream(new File("yourpathhere")));
You may find more information about how to use XWPF here.

Retrieve image path from assets folder

I have issue to retrieve image path from assets folder.
I have an image folder in assets folder. Within the folder i have three different folders.
Here is the code that i used:
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
try{
AssetManager mngr =getAssets();
InputStream BulletImgInput = mngr.open(IntroImage1);
//InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
Bitmap bitmapBullet = BitmapFactory.decodeStream(BulletImgInput);
BulletImage.setImageBitmap(bitmapBullet);
}catch(final IOException e){
e.printStackTrace();
}
I am wondering why can't i display the image? Because I have try to retrieve it through this code:
InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
It did retrieve the file, but with the string that i replaced in mngr.open it doesn't show up.
Really need you guys to help up.Thanks.
You don't need the AssetManager. You can do
BitmapFactory.decodeFile("file:///android_asset/image/Malay/bullet.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.
Update: Explanation
The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/image/Malay/bullet.jpg.
Try this:
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
Log.e("I/O ERROR","Failed when ..."
}
your BulletImage
String url = "file:///android_asset/NewFile.txt";
String url = "file:///android_asset/logo.png";
you can access any file....
InputStream BulletImgInput = mngr.open("file:///android_asset/image/Malay/bullet.png");
Maybe this could work for u.
May be problem is in missed image part of path?
String IntroImage1= "image/" + languageSelected + "/" + Intro1ImagePath + ".png" ;
instead of
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
Upd: Check values of languageSelected and Intro1ImagePath also.

Categories

Resources