Giving permission to another application to access my package

Asked

Viewed 215 times

1

I have an application that has a certain audio file, which is in /data/data/meu_pacote/audio_anexo/audio.mp3, so far so good, the problem is that the Mediaplayer class of Android fails to run the file, and when I put the same file on memory card, it performs smoothly.

I believe I eat the briefcase /data/data/meu_pacote belongs only to my application, Mediaplayer can not access the content that is inside it.

What is the solution?

Note: I have searched for Context.MODE_WORLD_READABLE.

Below is the code to create the audio file, which I am using. There is some parameter that I need to pass?

File diraudio = new File("/data/data/br.meupacote.meuapp/audio_anexo/");
//Verifica se o diretorio nao existe e cria ele;
if(!diraudio.exists())
{
    Log.i("XDEBUG","Pasta para áudio foi criada!");
    diraudio.mkdir();
}

String caminho = dir + audiofile.getName();

InputStream in = new FileInputStream(audiofile);
byte[] b = IOUtils.toBytes(in);

File file = new File(caminho);  

@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
  • Take a look at Contentproviders, maybe to share the files using this feature: http://developer.android.com/guide/topics/providers/content-providers.html

1 answer

1

I believe the biggest problem is making it impossible to read the file and its location. The solution I found when I needed to read a file saved with my project was to put the file in the folder: Myproject->Assets.

In order to read the file open the "mp3" file using Mediaplayer an example of a method I created is given:

 public void tocaMusica(String nomeFicheiro) throws IllegalArgumentException, IllegalStateException, IOException 
 {
     AssetFileDescriptor descriptor = null;
     try 
     {
        descriptor = getActivity().getAssets().openFd(nomeFicheiro);
     } 
     catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     player = new MediaPlayer();

     //Definimos duas variaveis com o inicio e o fim da reproducao do ficheiro que obetmos dos metodos getStartOffset e getLength
     long start = descriptor.getStartOffset();
     long end = descriptor.getLength();

     //Definimos o DataSource do player com o ficheiro que pegamos usando o AssetFileDescriptor e o inicio e o fim do ficheiro.
     player.setDataSource(descriptor.getFileDescriptor(), start, end);
     player.prepare();

     player.setVolume(1.0f, 1.0f);
     player.start();

    }

To play the song simply invoke the method tocaMusica with the file name inside the Assets folder as follows:

tocaMusica("teste.mp3");
  • Thanks for the suggestion, I chose to put the file on the same memory card, as I had to deliver the project. As soon as possible I will test your method. Thanks again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.