2010-12-07 2 views
2

ASTRO에서 액세스 할 수있는 Android에 mp3를 저장하는 프로그램을 만들려고합니다. 파일을/sdcard/media/audio에 저장하려고합니다. Logcat/DDMS에 대한 단일 오류없이 진행되는 응용 프로그램에도 불구하고 파일을 찾을 수 없습니다. WRITE_EXTERNAL_STORAGE 권한 (인터넷 등과 함께)을 추가하여 그렇게하지 않았습니다.Android : 파일을 sdcard에 저장 하시겠습니까?

앱이 sdcard에 파일을 저장하는 데 제한이 있습니까? 라는

방법 :

public void findSong(View view) throws Exception { 
    edittext = (EditText) findViewById(R.id.edittext);   
    searchTerm = edittext.getText().toString(); 
    String address;   
    address = getMusic(searchTerm);   
    ImageManager img = new ImageManager(); //calls ImageManager (below)   
    img.DownloadFromUrl(address, title + ".mp3"); 
} 

다운로드 방법 : 어떤 도움을 주시면 감사

private final String PATH = "/data/data"; //put the downloaded file here 

public void DownloadFromUrl(String imageURL, String fileName) { 
//this is the downloader method 

try { 
    URL url = new URL(imageURL); //you can write here any link 
    File file = new File(fileName); 
    long startTime = System.currentTimeMillis(); 
    Log.d("ImageManager", "download begining"); 
    Log.d("ImageManager", "download url:" + url); 
    Log.d("ImageManager", "downloaded file name:" + fileName); 

    /* Open a connection to that URL. */ 
    URLConnection ucon = url.openConnection(); 

    /* Define InputStreams to read from the URLConnection. */ 
    InputStream is = ucon.getInputStream(); 
    BufferedInputStream bis = new BufferedInputStream(is); 

    /* Read bytes to the Buffer until there is nothing more to read(-1). */ 
    ByteArrayBuffer baf = new ByteArrayBuffer(50); 
    int current = 0; 
    while ((current = bis.read()) != -1) { 
     baf.append((byte) current); 
    } 

    /* Convert the Bytes read to a String. */ 
    FileOutputStream fos = new FileOutputStream(PATH + file); 
    fos.write(baf.toByteArray()); 
    fos.close(); 
    Log.d("ImageManager", "download ready in" 
     + ((System.currentTimeMillis() - startTime)/1000) 
     + " sec"); 
    } catch (IOException e) { 
     Log.d("ImageManager", "Error: " + e); 
    } 
} 

.

+1

onCreate()에서 다운로드 할 필요가없는 정적 콘텐츠가 포함 된 더미 텍스트 파일을 작성하는 것과 같은 간단한 작업을 시도해보십시오. 일반적으로 좋은 생각은 아니지만 몇 가지 코드 만 사용하면됩니다. 파일을 쓰는 방법을 알아 냈는지 확인하십시오. –

+1

당신의 코드가 '/ data/data'에 파일을 넣고있는 것을 보여 주지만 질문에 '/ sdcard/media/audio'라고 쓰여 있습니다. – Squonk

답변

2

파일은 당신이해야 mp3 파일 인 경우 : 이것은 스캔을 FOCE 미디어 플레이어에서 사용할 수 있도록합니다

Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+path+filename))); 

은.

+0

예, 미디어 플레이어의주의를 환기 시키지만 Astro doesn 그것이 파일 관리자 인 것처럼 신경 쓰지 마라. –

+0

멋지다, 나는 이것을했고 폴더에서 파일을 찾는다. 그러나 "null.mp3"이라고하며 아무 것도 아닙니다. 그러나 그것은 엄청난 도움입니다. 이 줄은 어디에 있어야합니까? 어쩌면 그 이유는 null입니다. 나는 DownloadfromURL의 맨 끝에 그것을 넣었다. – GeauxSaints53

+1

@ GeauxSaints53 : 경로를 잘못 설정했습니다. – Macarse

관련 문제