MtpDevice.importFile and DocumentFile - android

MtpDevice.importFile(int objectHandle, String destPath) fails on 4.4 and above. Is there a way to import from an MtpDevice with the SD card write lock?

For the time being this is what I'm doing, though I'd love to avoid the double transfer:
// KitKat and higher require the extra step of importing to the cache then moving
if (Util.hasKitkat())
{
File tmp = new File(getExternalCacheDir(), name);
mMtpDevice.importFile(objectHandle, tmp.getPath());
success = FileUtil.moveFile(CameraImportActivity.this, tmp, endFile);
}
else
{
success = mMtpDevice.importFile(objectHandle, endFile.getPath());
}

Related

Crawling Android File System gets stuck in possible SymLink loop

I'm trying to crawl the entire file system of an android device, both directories and files, without the benefit of NIO, to build a tree of it. If I had NIO then I could use WalkTree or similar, but I don't.
The problem I am having (on the Nexus 5 API 23 x86 emulator) is in /sys/bus/pci/devices and possibly other directories (eg /proc/self) - it doesn't complete before the app times out/quits/crashes (unknown which), possibly getting into some kind of loop or something (the path may change in a repetitive fashion but the canonical path varies little or not at all) .
However if I rule out Symbolic links then that problem goes away but I get what is only some of the files on the device rather than all - for example lacking files under /data (or /data/media/0) and those files not showing up elsewhere - not to mention it looks completely different from the file system that most file managers show. The former is strange as I'd understood Symbolic Links pointed to files and folders that were still present in the file system, but just made them look as if they were elsewhere.
What's the solution? Do I have to code exceptions or special handling for /sys/bus/pci/devices, /proc/self and others? I'd prefer to keep Symbolic Links being followed if I can, and I'd prefer to crawl as many files and folders as I can (so starting in a sub-folder is not preferred).
And a few related questions that might affect the approach I eventually take - if I DO keep SymLinks then does that mean that some things will be crawled twice or more? Is there a way to avoid that? Is there a way to detect when something is the TARGET of a SymLink, other than following the SymLink and checking the CanonicalPath?
Here's my code:
I get the root (I understand that in Android, the first and likely only root is the valid one):
File[] roots = File.listRoots();
String rootPath = "";
try {
rootPath = roots[0].getCanonicalPath();
} catch (IOException e) {
// do something
}
Then I start the crawl (note the boolean to choose whether to ignore simlinks or not):
try {
// check if the rootPath is null or empty, and then...
File rootFile = new File(rootPath);
rootNode = new FileFolderNode(rootFile, null, true, false); // last param may be true to ignore sim links
//FileFolderNode(String filePath, FileFolderNode parent, boolean addChildren, boolean ignoreSimLinks)
} catch (Exception e) {
// do something
}
That uses the FileFolderNode, which has constructor:
public FileFolderNode(File file, FileFolderNode parent, boolean addChildren, boolean ignoreSimLinks) throws IOException {
if (file == null)
throw new IOException("File is null in new FileFolderNode");
if (!file.exists())
throw new IOException("File '" + file.getName() + "' does not exist in new FileFolderNode");
// for now this uses isSymLink() from https://svn.apache.org/repos/asf/commons/_moved_to_git/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java adjusted a bit to remove Java 7 and Windows mentions
if (!ignoreSimLinks)
if (FileUtils.isSymlink(file))
return;
this.name = file.getName();
if (this.name.equals("") && ! file.getCanonicalPath().equals("/"))
throw new IOException("Name is empty in new FileFolderNode");
this.isDirectory = file.isDirectory();
if (this.isDirectory) {
this.children = new ArrayList<FileFolderNode>();
if (addChildren) {
File[] files = file.listFiles();
if (files == null) {
// do something
} else {
// add in children
for (File f : files) {
FileFolderNode child = null;
try {
child = new FileFolderNode(f, this, addChildren, ignoreSimLinks);
} catch (Exception e) {
child = null;
}
if (child != null)
children.add(child);
}
}
}
}
}
Given the lack of answers here, I've broken this question down into areas needing clarification, and am trying to get answers to those - please do see if you can help with those:
Get Android Filing System root
Android SymLinks to hidden or separate locations or partitions
Avoiding Android Symbolic Link loop

Unable to make new folder in Apps cache folder

I have written a caching system. It saves data to the cache folder of the App using platform / Android methods to find the folder. I have an issue that I cannot create a new folder in the Apps cache folder.
This error is reporting in Crashlytics (via non-fatal error reporting) for some devices and not for the majority.
The method that contains the issue is my getCacheFolder() method.
An example folder path that does not work on these devices: /storage/emulated/0/Android/data/app.package.name/cache/stream-cache.
Reported versions affected: 4.4.2; 5.0.1; 6.0, 6.0.1.
Reported devices affected: LENOVO YOGA Tablet 2-1050L, MOTOROLA MotoE2(4G-LTE), Samsung SM-N910F (accounts for 98% of faults reported).
My suspicions are that there might be an issue relating to removable sdcards.
#NonNull
public File getCacheFolder() {
// If this is the first run of this library function, we should get our cache folder
if (mCacheFolder == null || !mCacheFolder.exists()) {
File dir = null;
// Prioritise the external sdcard to store cached files
dir = mContext.get().getExternalCacheDir();
if(dir == null) {
// There was no external sdcard - instead use internal storage
dir = mContext.get().getCacheDir();
}
// Still couldn't get a cache location, go to fallback plan and throw exception
if(dir == null) {
throw new IllegalStateException("Could not create a location to cache. " +
"This leaves the caching library in a bad state.");
}
// Point to our specific caching folder for this system
mCacheFolder = new File(dir, "stream-cache");
}
// If it doesn't exist, we will need to make it
if (!mCacheFolder.exists()) {
// If we cannot make the directory, go to fallback plan and throw an exception
if (!mCacheFolder.mkdirs()) {
// HERE IS WHERE THE FAULT IS REPORTED
throw new IllegalStateException("Could not create a location to cache. " +
"This leaves the caching library in a bad state.: "+mCacheFolder);
}
}
return mCacheFolder;
}
Update 1
I updated the code to write out various information about the state:
#NonNull
public File getCacheFolder() {
if (mCacheFolder == null || !mCacheFolder.exists()) {
File dir = mContext.get().getExternalCacheDir();
if(dir == null || !Utils.isExternalStorageWritable()) {
dir = mContext.get().getCacheDir();
}
mCacheFolder = new File(dir, "stream-cache");
}
if (!mCacheFolder.exists()) {
if (!mCacheFolder.mkdirs()) {
// Problems writing to cache - test writing to file path.
File filePath = mContext.get().getExternalFilesDir(null);
File cachePath = new File(filePath, "stream-cache");
throw new IllegalStateException("Could not create a location to cache. " +
"This leaves the caching library in a bad state.: "+mCacheFolder+ ". " +
"filePath="+filePath+". " +
"cachePath="+cachePath+". " +
"cachePath-exists="+cachePath.exists()+". " +
"cachePath-mkdirs="+cachePath.mkdirs());
}
}
return mCacheFolder;
}
The important results from this:
filePath=null - Context.getExternalFilesDir(null); returns a null. Breaking the rest of the tests.
Instead of using
// Point to our specific caching folder for this system
mCacheFolder = new File(dir, "stream-cache");
try using this:
// Point to our specific caching folder for this system
mCacheFolder = new File(dir + "/stream-cache");
in your first code snippets.

Could not initialize Tesseract API with language=eng

I am working on an Android app that requires OCR. I have decided to use Tesseract as API but I keep on getting this error:
E/Tesseract(native): Could not initialize Tesseract API with language=eng!
I have already copied file "eng.traineddata" to the location.
I am using Android Studio 2.1.2 (SDK 23)
Testing on device with API 22 Android Lollipop 5.1.1 (Read about Permission issue on Marshmallow)
Here is the code I am using:
public void reads(View view) {
TextView textView = (TextView) findViewById(R.id.textView);
int rotation = 0;
try {
ExifInterface exifInterface = new ExifInterface(mCurrentPhotoPath);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
switch (orientation){
case ExifInterface.ORIENTATION_ROTATE_90: rotation = 90; break;
case ExifInterface.ORIENTATION_ROTATE_180: rotation = 180; break;
case ExifInterface.ORIENTATION_ROTATE_270: rotation = 270; break;
}
} catch(Exception e) {
}
int w = imageBitmap.getWidth();
int h = imageBitmap.getHeight();
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,h,matrix,false);
} else {
imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,h);
}
imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888,true);
TessBaseAPI ReadIt = new TessBaseAPI();
ReadIt.init("/storage/emulated/0/","eng");
ReadIt.setImage(imageBitmap);
String Text = ReadIt.getUTF8Text();
if (Text!=null) textView.setText(Text);
}
I have used this line in my build.gradle dependency:
compile 'com.rmtheis:tess-two:6.0.2'
also, I have copied the"eng.traineddata in the folder named tessdata directly by downloading in the particular stated directory.
Tesseract-two isn't using the newest version of the OCR engine, it uses 3.05, so we are forced to use data from here. It seems the new data uses a different model, neural networks. The previous models before 4.0 worked differently.
I have tried using the data from here
and here. These data sets are only compatible with the newest version of tesseract, 4.0 (source), so it won't work if you are using an older version of tesseract.
Are you using tess-two?. In your code:
TessBaseAPI ReadIt = new TessBaseAPI();
ReadIt.init("/storage/emulated/0/","eng");
"/storage/emulated/0/" path should be pointing to your data files. You must have a subdirectory
named "tessdata". See
https://github.com/rmtheis/tess-two/blob/d7a45fd2e08b7ec315cd1e29d1a7e0c72fb24a66/tess-two/src/com/googlecode/tesseract/android/TessBaseAPI.java#L176
Read more at:
Could not initialize Tesseract API with language=eng!
Release permissions of manifest in Activity:
In manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
In onCreate:
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
}
If you dont use Marshmallow and still have problem try clean and rebuild project.
I had this same issue and the problem was that Marshmallow specifically requires a new way for your app to get read/write permission to storage. This blog post solved my problem.
In my Main Activity I have the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
getStorageAccessPermissions(); // Request storage read/write permissions from the user
}
#TargetApi(23)
private void getStorageAccessPermissions() {
int hasWriteStoragePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteStoragePermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_WRITE_EXTERNAL_PERMISSIONS);
}
}
Where REQUEST_CODE_WRITE_EXTERNAL_PERMISSIONS is an integer constant declared globally.
In a class that I have extending TessBaseAPI I added the following just for logging purposes to make sure that I actually can access the storage.
/* Checks if external storage is available to at least write to and returns the path name */
private static String isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
String retval = "External storage is not writable";
if (Environment.MEDIA_MOUNTED.equals(state)) {
retval = Environment.getExternalStorageDirectory().toString();
}
return retval;
}
/* Checks if external storage is available to at least read from and returns the path name */
private static String isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
String retval = "External storage is not readable";
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
retval = Environment.getExternalStorageDirectory().toString();
}
return retval;
}
use absolute path to tessdata directory from external storage (not assets)
for example if your models are in
/storage/emulated/0/Android/data/com.xxx.yyy/files/tessmodels/tessdata/
use this path
/storage/emulated/0/Android/data/com.xxx.yyy/files/tessmodels/
make sure you have write/read external storage permissions
use this model, tested with tess-two:9.0.0. I got it from tess-two sample app
Newer versions of tess-two check to make sure that the training data files can be found on the device. If those training data files are not found, a more informative message than the error message you're seeing will be shown.
So when you see this error message on newer versions of tess-two, it means that the training data files were found in the expected location, but they are the wrong version or are otherwise unreadable. Check to make sure you're using the right version of the training data files.

Samsung file io - can write file but not read back in

For several weeks, we've been fighting an issue that's been discussed on SO before, but without a working answer in the specific case we've encountered. So after re-reading dozens of threads and trying all the code folks offered, I'm asking for your assistance, please. (BTW, forget asking Samsung: they proved way less than helpful. )
OS : 4.0.x through 4.2.x (API lvls 14 - 17)
Devices : Samsung S3 which have /storage/sdcard0 (all we tested do.)
... as opposed to the older /mnt/sdcard or /storage/sdcard (note lack of trailing zero).
Use any flavor of
Environment.getExternalStorageDirectory().getAbsolutePath();
// e.g .getPath() .getName() etc.
// or
Environment.getExternalStoragePublicDirectory() ...
or use a media URI to inform system of file location. (Astounding that THAT fails too.)
Now save a file - you'll see it show up in the DDMS file explorer. And you can pull it via ADB to verify. But just try reading that same file back in - using the same app that you wrote it with in the first place ! ENOENT - file not found. Hardcode the path, append '/external_sd/' to the path the os call gives above (Sumsung says this is needed - but it doesn't do squat.)
Change 'sdcard0' to 'sdcard' as some have suggested ... tried all that too. ZILCH.
Permissions, etc are all correct of course ( since when could a process write a file but not read it !?) .
USB cable connected or not, debug mode or not, "real app" vs developer app (untrusted app) - results all the same: ENOENT )
Any ideas or suggestions on how to proceed here?
(With a sledge hammer in hand, staring intently at a new SG3 ... And, SAMSUNG, if you are reading this: "/storage/sdcard0/external_sd/myFileFoo.txt" does NOT work.)
/**
*
* [Edit - added sample of failing code, as requested]
*/
public void testFile () {
ImageView image ;
String m_Path = "/SamsuxS3/" ; // more fun than a barrel of NULLs
String m_MyFile = "myFileFoo.jpg" ;
image = (ImageView) findViewById ( R.id.imageView1 ) ;
//// Test 0:
m_Path = Environment.getExternalStorageDirectory().getAbsolutePath() ;
//// Test 1:
// String getPath = Environment.getExternalStorageDirectory().getPath(); // fails
// m_Path = getPath ;
//// Test 2:
// String getName = Environment.getExternalStorageDirectory().getName() ; //fails
// m_Path = getName ;
//// Test 3:
// String defPics = Environment.DIRECTORY_PICTURES; // fails
// m_Path = m_path + "/" + defPics + "/" ;
//// Test 4:
// m_Path = "/storage/sdcard0/" ; // fails
//// Test 5:
// m_Path = "/storage/sdcard0/external_sd/" // Samsung says so, but it fails too.
//// Test 6: now we're really hacking ...
// m_Path = "/storage/sdcard/" // Fails (although sdcard is mounted as sdcard0 - hmmm)
InputStream fIn = null;
File fileIn = new File(m_Path, m_MyFile);
try { //// This is only one way many attempts...
//// 1) just grab an image from a known resource,
//// 2) try to save it,
//// 3) then read it back into an ImageView.
//// External storage must be mounted or this fails.
InputStream is = getResources().openRawResource(R.drawable.somepicture) ; // works
OutputStream os = new FileOutputStream(fileIn); // OK
byte[] data = new byte[is.available()]; // OK
is.read(data); // OK
os.write(data); // OK - DDMS file explorer verfied
is.close();
os.close();
} catch (IOException e) {
Log.d("Error writing to " + m_Path, e.toString()); // never happened yet
}
//// now we step into the SamDung
////
InputStream fIn2 = null; //// Well, it's redundant but ...
File fileIn2 = new File(m_Path, m_MyFile);
try {
fIn2 = new FileInputStream (fileIn2) ;
//
// Here be the Dragons...
//
// Next line WORKS on every device EXCEPT a Samsung - blows up w/ ENOENT !
//
image.setImageBitmap ( BitmapFactory.decodeStream(fIn2) );
fIn2.close ();
} catch (Exception IOError) {
Log.d("WTF? I'm not moving to Korea: ", IOError.toString()) ;
}
}
The proper way to write a file on Android for Android 3.0+, where you intend to use the file again quickly, is:
os.flush();
os.getFD().sync();
os.close();
for a FileOutputStream named os.
Using this change, your example code works with Environment.getExternalStorageDirectory(), on a Samsung Galaxy S3, running Android 4.1.2.

Copy the shared preferences XML file from /data on Samsung device failed

There's an exporting feature in my application. It's just a copy operation since all my settings are store in shared preference.
I just copy the xml file from /data/data/package.name/shared_prefs/settings.xml to SD card. It works fine on my HTC desire. However, it might not work on Samsung devices, and i got the following error while I try to copy the file.
I/System.out( 3166): /data/data/package.name/shared_prefs/settings.xml (No such file or directory)
in the directory.
Anyone know how to fix it, or is there another simple way to store the shared preference ?
Thanks.
Never never never never never never never never never hardwire paths.
Unfortunately, there's no getSharedPreferenceDir() anywhere that I can think of. The best solution I can think of will be:
new File(getFilesDir(), "../shared_prefs")
This way if a device manufacturer elects to change partition names, you are covered.
Try this and see if it helps.
CommonsWare's suggestion would a be clever hack, but unfortunately it won't work.
Samsung does not always put the shared_prefs directory in the same parent directory as the getFilesDir().
I'd recommend testing for the existence of (hardcode it, except for package name):
/dbdata/databases/<package_name>/shared_prefs/package.name_preferences.xml and if it exists use it, otherwise fall back to either CommonsWare's suggestion of new File(getFilesDir(), "../shared_prefs") or just /data/data/<package_name>/shared_prefs/package.name_preferences.xml.
A warning though that this method could potentially have problems if a user switched from a Samsung rom to a custom rom without wiping, as the /dbdata/databases file might be unused but still exist.
More details
On some Samsung devices, such as the Galaxy S series running froyo, the setup is this:
/data/data/<package_name>/(lib|files|databases)
Sometimes there's a shared_prefs there too, but it's just Samsung's attempt to confuse you! Don't trust it! (I think it can happen as a left over from a 2.1 upgrade to 2.2, but it might be a left over from users switching roms. I don't really know, I just have both included in my app's bug report interface and sometimes see both files).
And:
/dbdata/databases/<package_name>/shared_prefs
That's the real shared_prefs directory.
However on the Galaxy Tab on Froyo, it's weird. Generally you have: /data/data/<package_name>/(lib|shared_prefs|files|databases)
With no /dbdata/databases/<package_name> directory, but it seems the system apps do have:
/dbdata/databases/<package_name>/yourdatabase.db
And added bonus is that /dbdata/databases/<package_name> is not removed when your app is uninstalled. Good luck using SharedPreferences if the user ever reinstalls your app!
Try using
context.getFilesDir().getParentFile().getAbsolutePath()
Best way to get valid path on all devices - run method Context.getSharedPrefsFile defined as:
/**
* {#hide}
* Return the full path to the shared prefs file for the given prefs group name.
*
* <p>Note: this is not generally useful for applications, since they should
* not be directly accessing the file system.
*/
public abstract File getSharedPrefsFile(String name);
Because of it hidden need use reflection and use fallback on fail:
private File getSharedPrefsFile(String name) {
Context context = ...;
File file = null;
try {
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = context.getClass().getMethod("getSharedPreferencesPath", new Class[] {String.class});
file = (File)m.invoke(context, new Object[]{name});
} catch (Throwable e) {
Log.w("App TAG", "Failed call getSharedPreferencesPath", e);
}
}
if (file == null) {
Method m = context.getClass().getMethod("getSharedPrefsFile", new Class[] {String.class});
file = (File)m.invoke(context, new Object[]{name});
}
} catch (Throwable e) {
Log.w("App TAG", "Failed call getSharedPrefsFile", e);
file = new File(context.getFilesDir(), "../shared_prefs/" + name + ".xml");
}
return file;
}
On some Samsungs implements like this:
public File getSharedPrefsFile(String paramString) {
return makeFilename(getPreferencesDir(), paramString + ".xml");
}
private File getPreferencesDir() {
synchronized (this.mSync) {
if (this.mPreferencesDir == null) {
this.mPreferencesDir = new File("/dbdata/databases/" + getPackageName() + "/", "shared_prefs");
}
File localFile = this.mPreferencesDir;
return localFile;
}
}
On other Android like this:
public File getSharedPrefsFile(String name) {
return makeFilename(getPreferencesDir(), name + ".xml");
}
private File getPreferencesDir() {
synchronized (mSync) {
if (mPreferencesDir == null) {
mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
}
return mPreferencesDir;
}
}
private File getDataDirFile() {
if (mPackageInfo != null) {
return mPackageInfo.getDataDirFile();
}
throw new RuntimeException("Not supported in system context");
}
After while Google change API for level 24 and later:
https://android.googlesource.com/platform/frameworks/base/+/6a6cdafaec56fcd793214678c7fcc52f0b860cfc%5E%21/core/java/android/app/ContextImpl.java
I've tested in Samsung P1010 with:
//I'm in a IntentService class
File file = this.getDir("shared_prefs", MODE_PRIVATE);
I got:
"/data/data/package.name/app_shared_prefs"
It works fine to me. I can run ffmpeg in this folder.
Look:
Context.getDir
You have to create the shared_prefs directory:
try{
String dir="/data/data/package.name/shared_prefs";
// Create one directory
boolean success = (new File(dir)).mkdirs();
if (success) {
// now copy the file
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
Also... the package of your app is package.name? Make sure you are referring to the right package.

Categories

Resources