Android: сохранить файл на SDCard? - PullRequest
2 голосов
/ 08 декабря 2010

Я пытаюсь создать программу, которая сохраняет mp3 на Android, доступ к которому может получить ASTRO.Я пытаюсь сохранить файл в / sdcard / media / audio.Несмотря на то, что приложение работает без единой ошибки в Logcat / DDMS, файл нигде не найден.Я добавил разрешение WRITE_EXTERNAL_STORAGE (вместе с Интернетом и т. Д.), Так что это не так.

Существуют ли ограничения для приложений, сохраняющих файлы на SD-карту?

Метод называется:

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 Ответ

2 голосов
/ 08 декабря 2010

Если файл представляет собой mp3, вы должны сделать:

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

Это приведет к сканированию и сделает его доступным в медиаплеере.

...