【问题标题】:I want to get a cursor to one file only. But how?我只想将光标移至一个文件。但是怎么做?
【发布时间】:2013-11-21 17:27:29
【问题描述】:

我是安卓新手。我只想将光标移至一个文件。但是怎么做?我试过了,但不起作用并给出错误。

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();
    }
}

【问题讨论】:

  • 感谢您的广告。我解决了我的问题。我稍后更新并告诉错误。
  • 但我不允许接受我自己的回复。你必须这样做!谢谢你。
  • 哦不。stackoverflow 告诉我明天我可以接受。这很有趣!哈哈。
  • 如果有帮助就给我投票吧!

标签: android cursor uri mediastore


【解决方案1】:

我终于找到解决问题的方法了。如果我们想将光标移动到一个特殊的文件上,首先在 Projection 数组中确定我们要投影的文件。我们必须定义想要获取其信息的 aur 文件。 我们使用带有“LIKE”字样的语句进行选择。代码在这里。同样,我了解到使用 CursorLoader 更好。因为不推荐使用 managedQuery 方法。 代码在这里:

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();

这是 CursorLoader 的正式形式。可能会对您有所帮助。

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);
}

【讨论】:

  • 如果有帮助,请查看。
猜你喜欢
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 2023-02-03
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
相关资源
最近更新 更多