Skip to content

Instantly share code, notes, and snippets.

@Steve-Mr
Created August 15, 2022 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Steve-Mr/744120422201c2440ab7e167c921cfe9 to your computer and use it in GitHub Desktop.
Save Steve-Mr/744120422201c2440ab7e167c921cfe9 to your computer and use it in GitHub Desktop.
/**
* The code below is from https://codestringz.com/share-intent-for-a-bitmap-without-saving-a-file/
*/
<!-- AndroidManifest.xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.codestringz.mytestapp">
<application
...
...>
<activity
...
.../>
<activity
...
.../>
<!--File Provider-->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>
/**
* The code below is from https://codestringz.com/share-intent-for-a-bitmap-without-saving-a-file/
*/
// MainActivity.java
@SuppressWarnings("ResultOfMethodCallIgnored")
private void shareBitmap(@NonNull Bitmap bitmap)
{
//---Save bitmap to external cache directory---//
//get cache directory
File cachePath = new File(getExternalCacheDir(), "my_images/");
cachePath.mkdirs();
//create png file
File file = new File(cachePath, "Image_123.png");
FileOutputStream fileOutputStream;
try
{
fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
//---Share File---//
//get file uri
Uri myImageFileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", file);
//create a intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_STREAM, myImageFileUri);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share with"));
}
}
/**
* The code below is from https://codestringz.com/share-intent-for-a-bitmap-without-saving-a-file/
*/
<!-- provider_paths.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-cache-path name="external_files" path="my_images/"/>
</paths>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment