अन्य ऐप्स के साथ फ़ाइलों को साझा करने के लिए अनुशंसित विधि ContentProviderFileProvider कहलाती है। दस्तावेज़ीकरण बहुत अच्छा है, लेकिन कुछ हिस्सों में थोड़ा मुश्किल है। यहां सारांश है।
प्रकट
<manifest>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
में FileProvider सेट अप अपने ऐप का पैकेज नाम के साथ com.example.myapp
बदलें।
रेस/xml/filepaths.xml बनाएं
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="shared_images" path="images/"/>
</paths>
यह जहां (इस मामले में कैश निर्देशिका का उपयोग) साझा करने के लिए फ़ाइलें प्राप्त करने FileProvider बताता है।
सहेजें आंतरिक भंडारण
// save bitmap to cache directory
try {
File cachePath = new File(context.getCacheDir(), "images");
cachePath.mkdirs(); // don't forget to make the directory
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
के चित्र को साझा करें छवि
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(Intent.createChooser(shareIntent, "Choose an app"));
}
आगे पढ़ने
यह क्यू एंड ए पढ़ने लायक है http://stackoverflow.com/questions/9049143/android-share-intent-for-a-bitmap-is-it-possible-not-to- save-it-pre-sharing – Suragch