setare shojaei
setare shojaei

Reputation: 145

I want to get a cursor to one file only. But how?

I am new to android. I want get cursor to one file only. But how? I tried this but doesn't work and gives error.

private void getallaudioFromMySpecialFolder() {   
    String[] star = { "*" };
    String Dir_recordedAudios= Environment.getExternalStorageDirectory()+File.separator+"My Audios"+File.separator;

    File Directory=new File(Dir_recordedAudios);

    Uri u=Uri.fromFile(Directory);

    Uri uri = Uri.parse(u.toString());

    Cursor musiccursor = managedQuery(uri, star,null, null, null);
    if (musiccursor!= null) {
        if (musiccursor.moveToFirst())  {
            do {
                String duration = musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String str_duration=CalTotalTime(Integer.valueOf(duration));
                String path= musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                Log.d("************", "**************");
                Log.d("path",path);
                Log.d("totalTime", str_duration);

            } while (musiccursor.moveToNext());
        }

        musiccursor.close();
    }
}

Upvotes: 0

Views: 2186

Answers (1)

setare shojaei
setare shojaei

Reputation: 145

I finally Find my question solving . If we want cursor to a special file,first in Projection array determine aour fileds for projection. And we must define aur file which want fetching its info. We use of a statement with "LIKE" word for selection. Code is here. Too,I learned that using from CursorLoader is better.because managedQuery method is deprecated. Code is here:

String name="";
    String duration="";
    String path="";
    Log.d("Loading file: " + filepath,"");

            // This returns us content://media/external/videos/media (or something like that)
            // I pass in "external" because that's the MediaStore's name for the external
            // storage on my device (the other possibility is "internal")
    Uri audiosUri = MediaStore.Audio.Media.getContentUri("external");

    Log.d("audiosUri = " + audiosUri.toString(),"");

    String[] projection = {MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.DURATION};

    // TODO This will break if we have no matching item in the MediaStore.
    Cursor cursor = managedQuery(audiosUri, projection, MediaStore.Audio.Media.DATA + " LIKE ?", new String[] { filepath }, null);
    cursor.moveToFirst();

    path =cursor.getString( cursor.getColumnIndex(projection[0]));
    name=cursor.getString( cursor.getColumnIndex(projection[1]));
    int iduration = cursor.getColumnIndex(projection[2]);
    duration=CalTotalTime(Integer.valueOf(iduration));
    Log.d("pathhhhhhhhhhhhhhhhhh", path);
    Log.d("nameeeeeeeeeeeeeeeeeeeeeee", name);
    Log.d("duration", duration);

    cursor.close();

And this is a formal of CursorLoader .May be helps you.

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Audio.Media.DATA };
    CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(proj[0]);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Upvotes: 1

Related Questions