Share file android github

Sharing a File

This lesson teaches you to

You should also read

Once you have set up your app to share files using content URIs, you can respond to other apps’ requests for those files. One way to respond to these requests is to provide a file selection interface from the server app that other applications can invoke. This approach allows a client application to let users select a file from the server app and then receive the selected file’s content URI.

This lesson shows you how to create a file selection Activity in your app that responds to requests for files.

Receive File Requests

To receive requests for files from client apps and respond with a content URI, your app should provide a file selection Activity . Client apps start this Activity by calling startActivityForResult() with an Intent containing the action ACTION_PICK . When the client app calls startActivityForResult() , your app can return a result to the client app, in the form of a content URI for the file the user selected.

To learn how to implement a request for a file in a client app, see the lesson Requesting a Shared File.

Create a File Selection Activity

To set up the file selection Activity , start by specifying the Activity in your manifest, along with an intent filter that matches the action ACTION_PICK and the categories CATEGORY_DEFAULT and CATEGORY_OPENABLE . Also add MIME type filters for the files your app serves to other apps. The following snippet shows you how to specify the new Activity and intent filter:

Define the file selection Activity in code

Next, define an Activity subclass that displays the files available from your app’s files/images/ directory in internal storage and allows the user to pick the desired file. The following snippet demonstrates how to define this Activity and respond to the user’s selection:

Respond to a File Selection

Once a user selects a shared file, your application must determine what file was selected and then generate a content URI for the file. Since the Activity displays the list of available files in a ListView , when the user clicks a file name the system calls the method , android.view.View, int, long)»>onItemClick() , in which you can get the selected file.

In , android.view.View, int, long)»>onItemClick() , get a File object for the file name of the selected file and pass it as an argument to getUriForFile() , along with the authority that you specified in the

element for the FileProvider . The resulting content URI contains the authority, a path segment corresponding to the file’s directory (as specified in the XML meta-data), and the name of the file including its extension. How FileProvider maps directories to path segments based on XML meta-data is described in the section Specify Sharable Directories.

The following snippet shows you how to detect the selected file and get a content URI for it:

Remember that you can only generate content URIs for files that reside in a directory you’ve specified in the meta-data file that contains the

element, as described in the section Specify Sharable Directories. If you call getUriForFile() for a File in a path that you haven’t specified, you receive an IllegalArgumentException .

Grant Permissions for the File

Now that you have a content URI for the file you want to share with another app, you need to allow the client app to access the file. To allow access, grant permissions to the client app by adding the content URI to an Intent and then setting permission flags on the Intent . The permissions you grant are temporary and expire automatically when the receiving app’s task stack is finished.

The following code snippet shows you how to set read permission for the file:

Caution: Calling setFlags() is the only way to securely grant access to your files using temporary access permissions. Avoid calling Context.grantUriPermission() method for a file’s content URI, since this method grants access that you can only revoke by calling Context.revokeUriPermission() .

Share the File with the Requesting App

To share the file with the app that requested it, pass the Intent containing the content URI and permissions to setResult() . When the Activity you have just defined is finished, the system sends the Intent containing the content URI to the client app. The following code snippet shows you how to do this:

Читайте также:  Удаление папки data android

Provide users with an way to return immediately to the client app once they have chosen a file. One way to do this is to provide a checkmark or Done button. Associate a method with the button using the button’s android:onClick attribute. In the method, call finish() . For example:

Источник

Share file android github

An Android app with the following purpose: when opening a file for viewing, gives the option to share it instead. I originally created this app for my own personal use. It was also a way for me to learn how to program on Android. Unfortunately it still has glitches and due to the Android architecture, I think it always will.

The root folder is a workspace which contains two projects: one which contains the main Android app, and the other which contains the unit tester. The projects were created using Eclipse ADT. They should be built and tested using the Eclipse ADT. The root folder structure is not standard (usually the root is a project, not a workspace) but it seemed best given that we need two projects.

I wrote limited automated unit test cases in the ShareFileAppTest project. This testing should be improved. These tests should be manually performed:

  • Change the settings. Make sure the activities are enabled/disabled as appropriate.
  • Make sure the links on the settings page work.
  • Make sure the activities receive files as expected (i.e. make sure the intent filters work as expected).
  • Make sure the activities generate and send the share/view intents appropriately.

Generate APK (Deployment)

Use the File | Export. option and choose «Export Android Application».

  • Per the Android guidelines, there should be no debug logging. Since this is a small project, I chose to just comment them out.

This project is released under GPLv3 (this is driven by the fact that it uses icons released under GPLv3). The license can be found in the root folder. The icon was built using icons available on Wikimedia Commons. See the icon folder for details and licenses.

Источник

Share file android github

Now implement LAN file sharing feature in your Android App using FileShare library with ease!

Adding FileShare to your Android project

The library is published in the jCenter repository. So you just add this in your App’s build.gradle file, in dependencies :

See the API Usage Wiki page

FileSharer is a Demo App that is built using FileShare library. You can see the source code in the FileSharerDemoApp folder. The APK for the Demo App is available in the releases.

I am yet to do some automation. These are the steps to use the App and to see how the Demo App works based on FileShare

Install the App in two Android devices.

In Receiver device :

  1. Create WiFi hotspot
  2. Open App
  3. click «Get File»
  4. The app should show a code on top of the button. And say «Listening» as a Toast

In Sender device :

  1. Connect to hotspot created by receiver
  2. Open App
  3. Type the code you saw in Receiver device
  4. Click «Pick File and Send»
  5. Pick a file (only docs can be picked) and select Done on top right

The file should get sent and you will see Toast messages accordingly in between for «Connected» , «Sending file», «Receiving file» etc in the two devices and finally the Receiver device will also show the location of the stored file as a Toast

The library is available under MIT License

About

[Not maintained] FileShare is an Android library to send and receive files among Android devices in a WiFi LAN

Источник

Share file android github

AnExplorer All-in-One File Manager

AnExplorer File Manager (File Explorer) is an all-in-one file management tools.

It is Simple, Small, Fast and Efficient File Explorer and One of the Best File Manager apps on Google Play Store. Only file manager to support RTL and show size of folders across the storages. File Manager is designed for all android devices including Phones, Phablets, Tablets and Android TV. It’s a fully designed with Material guidelines by Google. It supports all Android versions from Jellybean, Kitkat, Marshmallow to Nougat. We dont take any unnecessary permissions. The free version of AnExplorer has ads. You can buy the pro version for an adfree version which is less than a beer!

Читайте также:  Учебник по созданию андроид

Full Featured File Manager (File Explorer) It is a Super File Manager which provides all features of file management including cut, copy, delete, rename, compress, extract. You can check the file file details, full integrated search within the file system. New file and New folders can be created in a second.

Smart Library File Explorer (File Manager) Categorize all files into Downloads from internet, Bluetooth from nearby devices, Images, Pictures, Camera Photos, Videos, Movies, Audio , Music, Documents (pdf, xls, ppt etc), Archives (zip, rar etc) and APK. Easily navigate the library with a single tap. You can also bookmark all your favourite folders so you can have quick access at any point of time

Storage File Manager (File Explorer) Access all files and folder from different types of storages like EMMC, MMC cars, SD Cards, Internal Storage, External Storage , Phone Memory, USB Storage, Flash Drive, Pen Drives, USB OTG (Nexus and Non nexus) with support for FAT file system

Root File Manager (File Explorer) for advanced users to explore, edit, copy, paste and delete files in the root partition of phone storage for development purposes. Explore root folders of the system like data, cache. You phone needs to be rooted to use this feature

App Manager and Process Manager Free memory by killing tasks and speed up your phone instantly. Quickly and easily uninstall multiple unwanted applications and free up phone storage space which is very valuable. App Manager helps you save your favorite apps on to memory and install them back again when needed

Network Manager Transfer files with FTP Server from phone to PC. All you have to do is open the IP address in browser and all your files on the phone are easily accessible. You can also share files from your phone using bluetooth, wifi direct other social networking apps etc

Document Editor you can easily edit files on the go. Support for every type of text files such as HTML, XHTML, TXT etc. You can also edit files when the phone is rooted. It is very simple and handy.

Источник

Share file android github

Unity Native Share Plugin

This plugin helps you natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS. A ContentProvider is used to share the media on Android.

There are 5 ways to install this plugin:

  • import NativeShare.unitypackage via Assets-Import Package
  • clone/download this repository and move the Plugins folder to your Unity project’s Assets folder
  • import it from Asset Store
  • (via Package Manager) add the following line to Packages/manifest.json:
    • «com.yasirkula.nativeshare»: «https://github.com/yasirkula/UnityNativeShare.git»,
  • (via OpenUPM) after installing openupm-cli, run the following command:
    • openupm add com.yasirkula.nativeshare

NativeShare no longer requires any manual setup on Android. If you were using an older version of the plugin, you need to remove NativeShare’s

from your AndroidManifest.xml.

There are two ways to set up the plugin on iOS:

  • a. Automated Setup:(optional) change the value of Photo Library Usage Description at Project Settings/yasirkula/Native Share
  • b. Manual Setup: see: https://github.com/yasirkula/UnityNativeShare/wiki/Manual-Setup-for-iOS

Simply create a new NativeShare object and customize it by chaining the following functions as you like (see example code):

  • SetSubject( string subject ) : sets the subject (primarily used in e-mail applications)
  • SetText( string text ) : sets the shared text. Note that the Facebook app will omit text, if exists (see this topic)
  • SetUrl( string url ) : sets the shared url. On supported iOS apps, this url is used to generate a preview of the target webpage. Other iOS apps may append the url to the text or omit it. While sharing a file on iOS or while sharing anything on Android, the url is appended to the text (unless the text already contains the url)
  • AddFile( string filePath, string mime = null ) : adds the file at path to the share action. You can add multiple files of different types. The MIME of the file is automatically determined if left null; however, if the file doesn’t have an extension and/or you already know the MIME of the file, you can enter the MIME manually. MIME has no effect on iOS
  • AddFile( Texture2D texture, string createdFileName = «Image.png» ) : saves the texture to Application.temporaryCachePath with the specified filename and adds the image file to the share action
  • SetTitle( string title ) : sets the title of the share dialog on Android platform. Has no effect on iOS
  • AddTarget( string androidPackageName, string androidClassName = null ) : shares content on a specific application on Android platform. If androidClassName is left null, list of activities in the share dialog will be narrowed down to the activities in the specified androidPackageName that can handle this share action. Note that androidClassName, if provided, must be the full name of the activity (with its package). You can call this function multiple times. This function has no effect on iOS
  • SetCallback( ShareResultCallback callback ) : invokes the callback function after the share action is completed. ShareResultCallback has the following signature: void ShareResultCallback( ShareResult result, string shareTarget )
    • ShareResult result can take 3 values:
      • Unknown: we can’t determine whether or not the user has shared the content
      • Shared: user has probably shared the content. This value guarantees that the user has at least selected an app from the share sheet. But it is impossible to say whether the user has actually shared the content or cancelled the operation right after selecting the app from the share sheet
      • NotShared: either the user has closed the share sheet immediately or selected an app from the share sheet but then decided not to share the content (unfortunately, most apps return Shared for the latter case)
    • string shareTarget stores information about the app that the user has selected from the share sheet. It can be null or empty, if this information isn’t provided. Usually, this is the package name/class name of the selected application. You can use this value to e.g. determine if the user has picked Twitter from the share sheet: shareTarget != null && shareTarget.ToLowerInvariant().Contains( «twitter» )
Читайте также:  Mortal kombat ultimate для android

Finally, calling the Share() function of the NativeShare object will present the share sheet.

  • bool NativeShare.TargetExists( string androidPackageName, string androidClassName = null ) : returns whether the application with the specified package/class name exists on the Android device. If androidClassName is left null, only the package name is queried. This function always returns true on iOS
  • bool FindTarget( out string androidPackageName, out string androidClassName, string packageNameRegex, string classNameRegex = null ) : finds the package/class name of an installed application on the Android device using regular expressions. Returns true if a matching package/class name is found successfully. Can be useful when you want to use the AddTarget function but don’t know the exact package/class name of the target activity. If classNameRegex is left null, the first activity in the matching package is returned. This function always returns false on iOS
  • Can I share on a specific app?

On Android, you can share on a specific app via AddTarget. For iOS, you can check out this post and see if it works for you: https://forum.unity.com/threads/native-share-for-android-ios-open-source.519865/page-4#post-4011874

  • I can’t share image with text on X app

It is just not possible to share an image/file with text/subject on some apps (e.g. Facebook), they intentionally omit either the image or the text from the shared content. These apps require you to use their own SDKs for complex share actions. For best compatibility, I’d recommend you to share either only image or only text.

  • I can’t build the project to Android, it says «Android resource linking failed: unexpected element found in » in the error message

NativeShare adds element to AndroidManifest.xml due to the new package visibility change. The build error can be fixed by following these steps: https://developers.google.com/ar/develop/unity/android-11-build (in my tests, changing «Gradle installed with Unity» wasn’t necessary). In the worst case, if you are OK with NativeShare not working on some of the affected devices, then you can open NativeShare.aar with WinRAR or 7-Zip and then remove the . element from AndroidManifest.xml.

  • Can’t share, it says «java.lang.ClassNotFoundException: com.yasirkula.unity.NativeShare» in Logcat

If you are sure that your plugin is up-to-date, then enable Custom Proguard File option from Player Settings and add the following line to that file: -keep class com.yasirkula.unity.*

The following code captures the screenshot of the game whenever you tap the screen, saves it in a temporary path and then shares it:

  • Gif files are shared as static images on iOS (to learn more, please see this issue: https://github.com/yasirkula/UnityNativeShare/issues/22)

About

A Unity plugin to natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS

Источник

Оцените статью