2013-11-21 5 views
0

나는 안드로이드를 처음 사용하고 있습니다. 한 파일에만 커서를 가져오고 싶습니다. 그러나 어떻게? 나는 이것을 시도했지만 작동하지 않으며 오류를 준다.하나의 파일에만 커서를 가져 오려고합니다. 하지만 어떻게? (해결)

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

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

}는 귀하의 광고 주셔서 감사합니다. 나는 내 질문을 해결한다. 나중에 이것을 업데이트하고 오류를 알려준다. –

+0

하지만 내 자신의 응답을 수락하지 못했습니다. 당신은 그것을해야합니다! 감사합니다. –

+0

오 no.stackoverflow tomarrow 내가 받아 들일 수 말해.이 웃기는입니다! 하하. –

답변

0

마침내 내 질문 찾기. 커서를 특수 파일로 지정하려면 먼저 Projection array에서 투영을위한 필드를 결정합니다. 그리고 우리는 정보 가져 오기를 원하는 파일을 정의해야합니다. 선택을 위해 "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(); 

을 그리고 이것은 당신을하는 데 도움이 될 .May CursorLoader의 공식이다.

+0

도움이된다면 이것을 치워주세요. –