Saving public files in the internal storage

前端 未结 2 1907
日久生厌
日久生厌 2021-02-19 07:17

I have an application that saves files downloaded from a server. These files are not private to my application and should be accessible to other applications as well. I want to

2条回答
  •  你的背包
    2021-02-19 07:34

    I realize this is old and has an accepted answer, but I think the question is still relevant and that there is a way to achieve what the asker wants.

    To paraphrase, I believe the asker wants to save a file, and make it accessible (e.g. for viewing) by other apps, without having to think about external storage (e.g. an additional permission and the possibility that the external storage is not present).

    It turns out that you can make your own files readable by other apps.

    File file = new File( ctx.getCacheDir(), "picture" );               
    ...
    file.setReadable( true, false );
      // makes file readable to other apps
    
    // Get another app to view my picture
    Uri uri = Uri.fromFile(file);          
    Intent intent = new Intent( Intent.ACTION_VIEW );
    intent.setDataAndType( uri, "image/*" );
    ctx.startActivity( intent );
    

    The setReadable made the above code work for me - without that line the viewer app couldn't read the file. Note that setReadable operates on the whole directory, so the best solution is probably to create a sub-directory name 'public' and put your files in there.

提交回复
热议问题