Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android share images from assets folder

Tags:

I'm trying to share an image from my assets folder. My code is:

Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/jpg"); share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///assets/myImage.jpg")); startActivity(Intent.createChooser(share, "Share This Image")); 

but it doesn't work. Do you have any ideas?

like image 244
Buda Gavril Avatar asked May 04 '11 19:05

Buda Gavril


People also ask

How do I access asset files on Android?

Step 3 – Right click app >> New >> Folder >> Assets folder. Right click on the assets folder, select New >> file (myText. txt) and your text.

What is the use of asset folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.


1 Answers

It is possible to share files (images including) from the assets folder through a custom ContentProvider

You need to extend ContentProvider, register it in your manifest and implement the openAssetFile method. You can then assess the assets via Uris

    @Override     public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {         AssetManager am = getContext().getAssets();         String file_name = uri.getLastPathSegment();          if(file_name == null)              throw new FileNotFoundException();         AssetFileDescriptor afd = null;         try {             afd = am.openFd(file_name);         } catch (IOException e) {             e.printStackTrace();         }         return afd;     } 
like image 151
smith324 Avatar answered Oct 28 '22 09:10

smith324