I've developed a Backup application. Now, it could takes backup for contacts, settings and browser I would take these backups by that host like
Backup bu = new Backup(this);
bu.runBackup(Contacts.People.CONTENT_URI);
bu.runBackup(Settings.System.CONTENT_URI);
bu.runBackup(Browser.SEARCHES_URI);
I've use gethost method in Backup class like
int count=0;
String file = uri.getHost() +"-"+ System.currentTimeMillis();
Cursor cursor = cr.query(uri, null, null, null, null);
count = cursorToCSV(cursor, file);
cursor.close();
String msg = String.format("Backed up %d records to %s file", count, file);
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
return count;
I would like to take backup for media files images, videos, musics Is it possible to do that? How can i do like this? Anyone knows mean tell me otherwise what's the alternate way?
I've done this by my own Coding...
bu.runBackup(Images.Media.CONTENT_URI);
bu.runBackup(Video.Media.CONTENT_URI);
bu.runBackup(Audio.Media.CONTENT_URI);
Related
Using this method of audio file retrieval from Android's external storage
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
can I actually find a resonable way to fetch a genre of the given song? MediaStore class seems to provide everything else - from song's title to its composer info - except for the genre field. Should I use MediaMetadataRetriever then? If so, how drastically can creating a MediaMetadataRetriever instance for every song on a device reduce app's performance?
Maybe there are some better ways to retrieve all audio files from both external and internal storages in android?
As mentioned at Developer's Site,
You can fetch the Genres of the Audio file using MediaStore.Audio.Genres
Sample Code :
private static String[] genresProj = {
MediaStore.Audio.Genres.NAME,
MediaStore.Audio.Genres._ID
};
int idIndex = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
while (cursor.moveToNext()){
int id = Integer.parseInt(mediaCursor.getString(idIndex));
Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId("external", id );
genresCursor = context.getContentResolver().query(uri,
genresProj , null, null, null);
int genreIndex = genresCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);
while (genresCursor.moveToNext()) {
Log.d(TAG, "Genre = " +genresCursor.getString(genreIndex));
}
}
}
To fetch other details of the Audio file, please check here .
I need to open a database from my phone's memory (/storage/emulated/0) and I've got "SQLiteCantOpenDatabaseException" the database to open (created by DB browser from linux) have the tables "android_metadata" and them respective fields (locale...es_US).
I need open and read data from the external db (the app need to download the db from website and read data and put in textview into the app activities )
The app has permission to read and write external memory
code below (only the code of button to get the database reference)
case R.id.button4:
{
int ID,IDn,IDa;
String cabeceras[] ={"nombre","apellido",_ID},lectura=new String(),err="";
ArrayList<String> arregloDatos = new ArrayList<>();
try {
SQLiteDatabase db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory().getPath() +"/"+"pruebaDB.db", null, SQLiteDatabase.OPEN_READWRITE);
Cursor cursor = db.query("persona", cabeceras, null, null, null, null, null);
ID = cursor.getColumnIndex(_ID);
IDn = cursor.getColumnIndex("nombre");
IDa = cursor.getColumnIndex("apellido");
while (cursor.moveToNext()) {
lectura = cursor.getString(ID) + " " + cursor.getString(IDn) + " " + cursor.getString(IDa);
arregloDatos.add(lectura);
}
Intent i = new Intent(this, Main2Activity.class);
i.putStringArrayListExtra("cadena", arregloDatos);
startActivity(i);
}catch (Exception g)
{
Toast msg =Toast.makeText(this,g.getClass().toString(),Toast.LENGTH_SHORT);
msg.show();
}
break;
}
Edit: i find the problem, the problem is the cursor, becose i comment it and the app not show any exception.
now...my problem is "how to read data from SQliteDatabase object"
I don't think you are able to write SQLDatabases to external. If it is all string or int data, you could write the Database to internal, then write a function that will iterate through the table and concatenate the data and write it to external as a .txt.
Yep, just read the documentation on the .openDatabase method. I think the first param is looking for the path portion of the content URI, not an ext memory file path.
https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openDatabase(java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int,%20android.database.DatabaseErrorHandler)
I'm trying to access system folders on Android, /Music to be precise. What I found in documentation is that getExternalFilesDir(Environment.DIRECTORY_MUSIC) will grant access to music folder related to my app, which is part of it and eventually will be deleted once app gets removed from the device. So my question is how to access system music folder contents? Will I have the necessary permissions to grab these files and send over Bluetooth later on?
What I have done so far, yet it obviously lists app directory:
File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
Log.d("Files", "Path: " + path);
File f = new File(path,"");
File file[] = f.listFiles();
for (int i=0; i < file.length; i++)
{
filesList.setText("FileName:" + file[i].getName());
}
To get a list of all the music on a device, you'll want to look into using Content Providers and MediaStore
Check out this code from Google's sample code for how to retrieve music from a device. It shows you exactly how to do it.
The important bit is this part...
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Perform a query on the content resolver. The URI we're passing specifies that we
// want to query for all audio media on external storage (e.g. SD card)
Cursor cur = mContentResolver.query(uri, null,
MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
if (cur == null) {
// Query failed...
return;
}
if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
return;
}
// retrieve the indices of the columns where the ID, title, etc. of the song are
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
I have videoplayer app with filebrowser listing all videos on SD card
Code inspired by i want get audio files in sd card
Using ContentResolver, works as expected, but it does not update if the files on card change. I do not mean automatically, but after view/app restart. Not even reinstalling the application helped, still shows the same files. The deleted video file is not visible via PC nor it is possible to play it (This video cannot be played (translation)).
I dumped the data and the problem is not in view caching or elsewhere. I do not implement any caching of my own and failed to find anything on the matter. Thank you
Code:
// acquisition
String[] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATA
};
ContentResolver resolver = getActivity().getContentResolver();
Cursor videoCursor = resolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null
);
// extraction
while(cursor.moveToNext()) {
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
filepath = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
filename = cursor.getString(cursorIndex);
cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
duration = cursor.getString(cursorIndex);
result[ index++ ] = new VideoFileMetadata(filename, duration, filepath);
}
Edit 1 [14-03-2013]:
I tried adding number + " = " + number to ORDER or WHERE clause to act as a potential query caching buster, but it had no effect (although it's possible it was removed by an optimizer as a useless clause). This time I had reinstalled the application from a different machine using different certificate, but the query result remained the same, listing currently non-existing files.
You should first call cursor.moveToFirst() .
So, your cursor iteration loop should look like
if (cursor.moveToFirst()) {
do {
// cursorIndex = cursor.getColumnIndexOrThrow, etc...
} while (cursor.moveToNext());
}
A project I'm currently working on requires the application to discover all audio tracks on an android device. In addition to the tracks, it must also be able to group them by album and artist.
I found the MediaStore content provider and set about creating a database helper utility class to quickly return the IDs of tracks, albums and artists. I have been able to query the Media Store which returns some result, but it appears that not all of the audio information is stored there.
For example, querying for all artists returns only 9 results, but the Android Music Player application returns 27.
I am using the following code to query the artists:
ContentResolver resolver = getContentResolver();
String[] projection = new String[]{MediaStore.Audio.ArtistColumns.ARTIST};
Uri uri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor c = null;
try {
c = resolver.query(uri, projection, null, null, null);
}
catch (UnsupportedOperationException e) {
Log.e("DatabaseHelper", "query: " + e);
c = null;
}
if(c != null) {
while(c.isAfterLast() == false) {
Log.e("ARTIST", "NAME: " + cursor.getString(0));
cursor.moveToNext();
}
}
It seems as if the Media Scanner (which is definatley run when my device boots up) is not detecting much of my audio library. Am I doing something wrong?
I am simply trying to find all audio tracks, audio albums and audio artists quickly and efficiently. If MediaStore can't help me then I fear I will have to implement some form of file scanner to traverse directory structures and build my own database, but I don't want to do that ;)
Any thoughts would be much appreciated.
Thanks.
Probably, You should also query android.provider.MediaStore.Audio.Albums.INTERNAL_CONTENT_URI to get the rest visible in media player.