R
R
Rou19972017-01-12 00:31:55
Java
Rou1997, 2017-01-12 00:31:55

Android Dev: Saving data to SD card for MediaTek ("Portable Player") based devices?

There are many budget smartphones and tablets based on MediaTek, in which the chipset chip has a model of the MTxxxx type, for example MT6571.
"Chip set" - "a set of chips in one microcircuit package", in particular, there is a CPU, GPU and others, and there is also a controller that "goes out" to the USB connector and provides access to data.
In devices with different chipsets, respectively, this controller also differs.
Let's say, for Qualcomm-based smartphones, the approach to implementing this controller is correct.
There is the same controller as in USB flash drives, the smartphone is recognized as a "Removable disk", and uses the appropriate "protocol" for data exchange with the PC file system driver, the implementation of which works well, and I do not remember any "jambs",
But for some reason, MediaTek decided to use a different technology - the one in MP3 players. This is MTP, and its implementation by Windows is WPD.
Probably, Media Tek previously dealt with multi media and did not want to (as it seemed to them) "reinvent the wheel". There are many such cases.
And this system is "sharpened" specifically for working with multimedia, and for a smartphone or tablet, which is a full-fledged multifunctional computer, it does not fit well.
First of all, when connecting to a PC and opening the "SD card", not only do we have to click an extra time to open the device first, and then the map (and we still can't get the full path to the map), but in addition it loads very slowly file tree.
But that's what, but let's try to save the file to the SD card from our Android application:

//android.permission.WRITE_EXTERNAL_STORAGE
new File(Environment.getExternalStorageDirectory(), "file.txt").createNewFile();

Let's connect the device to the PC - and what? The file seems to be missing, although on the device itself, all applications already have access to it.
It does not help even to remove the cable and insert it again. Only rebooting the device...
However, in system applications, for example, "Camera", saving the file works fine - the file immediately appears on the PC, even if the device is connected to it at that moment.
I had an idea - to see how it is done in this application, and copy it.
I believe that there just happens an alert MediaStore, which on this device notifies the controller, and it already notifies the PC.
But even if otherwise, then in any case it is certainly possible to copy.
But will there be "pitfalls" on other devices?
Has anyone actually tried to solve this problem? It's not the norm. :)
UPD: Such a solution has been developed, exactly with MediaStore, as expected:
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, fileabsolutepath);
context.getContentResolver().insert(Files.getContentUri("external"), values);

Tested on several different devices, it performs its task, but on the PC to which the device was connected for a long time and operations were performed, the update stopped working, I took out the cable, inserted it again, they worked again, but later found that for some reason Windows Media Player stopped working and had to restart.
This may not happen if you specify the file type as "unknown", or if you use another way to add to MediaStore.
We will have to study MTP further, as well as continue testing on our devices - and on the devices of the users of the application. :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-01-19
@DrZ0idberg

Your thinking is correct, but the implementation is not entirely correct. You really need to notify MediaStore about the appearance of a new file (and deletion, of course, too). But it's done like this:

String fileAbsPath = "file.txt";
new File(Environment.getExternalStorageDirectory(), fileAbsPath).createNewFile();
MediaScannerConnection.scanFile(context, new String[]{fileAbsPath}, null, null);

Details here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question