Share Intent of Google+ can not access image - android

i am calling an intent to share an image. this works with most providers, BUT with Google+. Google+ opens the post activity without the image and displays the toast "You can only post photos stored on your device." at the same time.
File f = storeImage(image); // f = /data/data/com.myapp/files/1333070776978.jpg
Uri uri = Uri.fromFile(f);
Intent share = new Intent(Intent.ACTION_SEND);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TITLE,"Share that title");
share.putExtra(Intent.EXTRA_SUBJECT,"Share that subject");
share.putExtra(Intent.EXTRA_TEXT,"Check that out...");
share.putExtra("sms_body", "sms body");
startActivity(Intent.createChooser(share, "Share Image"));
i save the image with
Context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
my understanding was that by setting FLAG_GRANT_READ_URI_PERMISSION, i give Google+ specific access to this file.
it works when i store the image into the MediaStore, but i actually don't wanna clutter the users image gallery.
ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
any advice is appreciated
Simon

Google+ cannot access file:// Uris from the private folder of another application. And the FLAG_GRANT_READ_URI_PERMISSION doesn't work in this case because it's only for Uris in the "data" part of an Intent, but not for extras.
One common workaround is to add it to the gallery (via the MediaStore) but it's not necessary to do so. The correct solution in this case is to use a FileProvider:
FileProvider is a special subclass of ContentProvider that facilitates
secure sharing of files associated with an app by creating a
content:// Uri for a file instead of a file:/// Uri.
First place the following in your AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.testshare.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
Then this file in res\xml\filepaths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="shared_images" path="shared_images/" />
</paths>
Then, to share an image file, make sure it's in the path specified above (i.e. shared_images under getFilesDir()) and build the intent as follows:
File file = getImageFileToShare();
Uri fileUri = FileProvider.getUriForFile(this, "com.myapp.testshare.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.setType("image/png");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
(Make sure the authority specified in the getUriForFile() method matches the one in the manifest).
This will produce a content:// Uri (like content://com.myapp.testshare.fileprovider/shared_images/img1.png that the Google+ app will be able to access, and thus include in the post).

Simon,
Seems as if it works to set the permission after you set the uri.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try this one, I'm also having problems with this as its showing up in Gallery if I do an insert..
//out here is a File instance
String uriString = MediaStore.Images.Media.insertImage(getContentResolver(),
out.getAbsolutePath(),null,null);
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse(uriString));
https://github.com/google-plus/google-plus-office-hours/blob/master/2012_08_14-curiosity-android-app/src/com/example/CuriosityActivity.java

If you are using a custom ContentProvider you must check that the MIME type of the images is correct. The correct MIME for a jpeg is "image/jpeg". If you return "image/jpg" in the overridden getType(Uri uri); method then Google+ fails to attach them. Most other one work, e.g. messaging and email. "image/jpg" is NOT a valid IANA MIME type:
http://www.iana.org/assignments/media-types/image
The specification of getType is clear about this, but it is very easy to miss (I did). Having solved this, I wanted to ensure others spot this because it is very unobbious.

You can use this :
Drawable drawable;
File file;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
drawable = getResources().getDrawable(imgResId);
} else {
drawable = getResources().getDrawable(imgResId, null);
}
if (drawable != null) {
Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
String path = Environment.getExternalStorageDirectory()
.toString();
OutputStream fOut = null;
file = new File(path, "pic.jpg"); // the File to save to
try {
file.createNewFile();
fOut = new FileOutputStream(file);
// obtaining the Bitmap
bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Related

Captured Image is not storing in android 11

I'm unable to store captured image in (getExternalFilesDir(Environment.DIRECTORY_PICTURES)) Android 11 device.
I have added
<uses-permissionandroid:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> in manifest and all file access also. But it's not working.
if (Build.VERSION.SDK_INT >= 30) {
if (!Environment.isExternalStorageManager()) {
try {
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse(String.format("package:%s", applicationContext.packageName))
startActivityForResult(intent, 2296)
} catch (e: Exception) {
val intent = Intent()
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
startActivityForResult(intent, 2296)
}
}
}
This code is working below Android 11 device. But on Android 11 file is not creating File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) .toString() + "/" + FolderName )
Your phone's camera doesnot have permission to write in the specified location. So to fix this, you need to use file provider and give it appropriate permissions so that the camera can write the image to your file.
To do that,
create a FileProvider. In your manifest file, add:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" /> // <-------- see this
</provider>
Now create a files.xml file in your res/xml folder. In it, write some code:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name="camera"
path="Camera/" />
<cache-path
name="cache"
path="/" />
<files-path
name="files"
path="." />
<external-path
name="external"
path="." />
<external-files-path
name="my_images"
path="/"/>
// todo: add necessary folders according to your requirements...
// also, this is an old example. Consider googling for the latest style. I'm just copying from an old project I have, and it kinda works...
</paths>
So here we are giving the FileProvider the folders that can be shared with external apps.
2. Now create a uri where you want to store the photo. in your activity:
Context applicationContext = getApplicationContext();
File root = getCachedDir(); // consider using getExternalFilesDir(Environment.DIRECTORY_PICTURES); you need to check the file_paths.xml
File capturedPhoto = new File(root, "some_photo.jpeg");
if(!photoFile.exists()) {
photoFile.mkdirs();
}
Uri photoURI = FileProvider.getUriForFile(applicationContext, applicationContext.getPackageName() + ".fileprovider", capturedPhoto);
Please note that my project needed to save picture temporarily, so I had used cachedDir. If you save photo permanently, use getExternalFilesDir(Environment.DIRECTORY_PICTURES); and modify file_paths.xml properly.
Now that we have the correct uri, we can call the camera intent:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startActivityForResult(takePictureIntent, REQUEST_CODE);
Finally, in activty result, do something:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
// todo: maybe show photo in an imageView
}
}
I hope this works.
Edit
If you are using this app in production, relying on android's default camera app is a bad idea. Our app previously used this way, and it works with, say, samsung's defaul camera. But a lot of our users used third party apps, such as PixArt, which doesnot save photo to our given location. So we had to implement a builtin camera using CameraX. So consider using CameraX or some other camera library.
First Thing is "android.permission.MANAGE_EXTERNAL_STORAGE" Permission has no relation with saving image.
after Android 11 google say you should do your business in your space.
that mean you cant get or save image or any file as you did before Android 11.
you can only save in Shared folder or in your application storage data/App packagename/.....
if you want to access other app files then you need "android.permission.MANAGE_EXTERNAL_STORAGE" but google say this must be app prior functionality like filemanager or virus scanner like app.
As far as your App concern you havent provide save code.
in Android 11 i am suggestion using Media Api
ContentResolver resolver = mContext.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, s);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + File.separator + getResources().getString(R.string.app_name) + File.separator + "imgfolder");
contentValues.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
contentValues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
try {
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
contentValues.clear();
contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
resolver.update(imageUri, contentValues, null, null);
}
this is for image file
use this code for save captured image
String mPath = Environment.getExternalStorageDirectory() + "/Print";
Bitmap tmp = BitmapFactory.decodeFile(mPath);
File imageFile = new File(mPath);
FileOutputStream outStream;
try
{
outStream = new FileOutputStream(imageFile);
try
{
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outStream);
outStream.flush();
outStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
} catch (Exception e)
{
e.printStackTrace();
}

How to read files stored in cache from other applications? [duplicate]

I am trying to share image file in cache directory, i have the complete path, but not able to send the file in attachments, the code is
File shareImage=Utils.getBitmapFile();
Log.d("Activity", "get final path in result"+shareImage.getAbsolutePath());
/*MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=shareImage.getName().substring(shareImage.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
shareIntent.setType(type);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri shareImageUri = Uri.fromFile(shareImage);
Uri shareImageUri = Uri.fromParts("content", shareImage.getAbsolutePath(), null);//("content://"+shareImage.getAbsolutePath());
*/
Uri shareImageUri = Uri.fromFile(shareImage);
Log.d("Result ","uri is "+shareImageUri.toString());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Results"));
the above commented code is not working
the send mail shows attachment,but not receiving end there is not attachment,
facebook sharing also shows no image in post
what the reason for this??
I have already seen the following SO Links how-to-use-share-image-using-sharing-intent-to-share-images-in-android and many others, none of them are able to resolve the issue
P.S.;
1.The aim is to take screenshot of screen save it in cache directory and share it online from there
2.Yes i do have file, I can pull it via DDMS from device and see on system.
I followed #CommonsWare's advice and used a FileProvider. Assuming your image is already in the internal cache directory as cache/images/image.png, then you can use the following steps. These are mostly a consolidation of the documentation.
Set up the FileProvider in the Manifest
<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>
Replace com.example.myapp with your app package name.
Create res/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>
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Share the image
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"));
}
Documentation
FileProvider
Storage Options - Internal Storage
Sharing Files
Saving Files
what the reason for this?
As noted, other apps do not have access to your app's internal storage.
none of them are able to resolve the issue
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what specific solutions you have tried and what specific problems you have encountered.
but that does not seems to be working as per SO post!!!
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what "that does not seems to be working" means.
Or, use FileProvider, which offers this capability with no code required beyond an entry for it in your manifest.
Or, store your image on external storage, such as getExternalCacheDir().
I share cached image by followed steps .
1.Copy your cached image to target path.
public static File copyImage(String sourcePath, String targetPath){
try {
InputStream in = new FileInputStream(sourcePath);
OutputStream out = new FileOutputStream(targetPath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return new File(targetPath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
2.Get the Uri of copy file.
Uri uri = Uri.fromFile(target);
3.Share image by intent
File dir = new File(Constant.INKPIC_PATH);//your custom path,such as /mnt/sdcard/Pictures
if(!dir.exists()){
dir.mkdirs();
}
File f = new File(dir,"temporary_file.jpg");
File target = copyImage(url,f.getAbsolutePath());
Uri uri = Uri.fromFile(target);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,uri );
context.startActivity(intent);

Share Image File from cache directory Via Intent~android

I am trying to share image file in cache directory, i have the complete path, but not able to send the file in attachments, the code is
File shareImage=Utils.getBitmapFile();
Log.d("Activity", "get final path in result"+shareImage.getAbsolutePath());
/*MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext=shareImage.getName().substring(shareImage.getName().lastIndexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
shareIntent.setType(type);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
Uri shareImageUri = Uri.fromFile(shareImage);
Uri shareImageUri = Uri.fromParts("content", shareImage.getAbsolutePath(), null);//("content://"+shareImage.getAbsolutePath());
*/
Uri shareImageUri = Uri.fromFile(shareImage);
Log.d("Result ","uri is "+shareImageUri.toString());
shareIntent.putExtra(Intent.EXTRA_STREAM, shareImageUri);
startActivity(Intent.createChooser(shareIntent, "Share Results"));
the above commented code is not working
the send mail shows attachment,but not receiving end there is not attachment,
facebook sharing also shows no image in post
what the reason for this??
I have already seen the following SO Links how-to-use-share-image-using-sharing-intent-to-share-images-in-android and many others, none of them are able to resolve the issue
P.S.;
1.The aim is to take screenshot of screen save it in cache directory and share it online from there
2.Yes i do have file, I can pull it via DDMS from device and see on system.
I followed #CommonsWare's advice and used a FileProvider. Assuming your image is already in the internal cache directory as cache/images/image.png, then you can use the following steps. These are mostly a consolidation of the documentation.
Set up the FileProvider in the Manifest
<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>
Replace com.example.myapp with your app package name.
Create res/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>
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Share the image
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"));
}
Documentation
FileProvider
Storage Options - Internal Storage
Sharing Files
Saving Files
what the reason for this?
As noted, other apps do not have access to your app's internal storage.
none of them are able to resolve the issue
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what specific solutions you have tried and what specific problems you have encountered.
but that does not seems to be working as per SO post!!!
Feel free to open a fresh StackOverflow question, where you explain, completely and precisely what "that does not seems to be working" means.
Or, use FileProvider, which offers this capability with no code required beyond an entry for it in your manifest.
Or, store your image on external storage, such as getExternalCacheDir().
I share cached image by followed steps .
1.Copy your cached image to target path.
public static File copyImage(String sourcePath, String targetPath){
try {
InputStream in = new FileInputStream(sourcePath);
OutputStream out = new FileOutputStream(targetPath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return new File(targetPath);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
2.Get the Uri of copy file.
Uri uri = Uri.fromFile(target);
3.Share image by intent
File dir = new File(Constant.INKPIC_PATH);//your custom path,such as /mnt/sdcard/Pictures
if(!dir.exists()){
dir.mkdirs();
}
File f = new File(dir,"temporary_file.jpg");
File target = copyImage(url,f.getAbsolutePath());
Uri uri = Uri.fromFile(target);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,uri );
context.startActivity(intent);

Error while reading pdf using MODE_WORLD_READABLE?

I have made an app which downloads the pdf from the server and stores it in
/data/data/<package_name>files
using this code:
FileOutputStream fos = openFileOutput(pdfFileName, Context.MODE_WORLD_READABLE);
fos.write(pdfAsBytes);
fos.close();
But when reading these file from the pdf reader app which I already have on the device is sometimes showing black screen and sometimes displays the file with annoying fonts. The code I am using is:
File file = new File("/data/data/<package_Name>/files/pdffile");
Uri path = Uri.fromFile(file);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(Exception e)
{
Log.e("Activity Not Found Exception",e.toString());
}
I have tried the same code with the same files at other path(in sdcard) they work fine.
Please help me and tell me what should have gone wrong.
What should be a possible way to correct it?
You should use method
android.content.Context.getFilesDir().
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
This code works for me:
File file = new File(this.getFilesDir(), pdfFileName);
Uri path = Uri.fromFile(file);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(pdfIntent);
} catch(ActivityNotFoundException e) {
Log.e("Activity Not Found Exception",e.toString());
}
There are only 2 methods to achieve what you want:
1) Using SDCARD
You save downloaded pdf somewhere SDCARD. Something like this:
Instead of creating a file:
File pdfFile = new File(Environment.getExternalStorageDirectory(), "pdffile");
FileOutputStream fos = new FileOutputStream(pdfFile);
fos.write(pdfAsBytes);
fos.close();
...
And you use pdfFile when creating an Intent.
2) Using custom ContentProvider
The second method can be used if you still do not want to use your SDCARD storage. You can use your app's CacheDir (you will download your file there). This involves creating your own ContentProvider, and after that you will be able to pass corresponding URI to the pdfreader app.
Details on how to do this all are here.
There is no known other methods to open any downloaded file in 3rd party app in Android.

Android Share Intent for a Bitmap - is it possible not to save it prior sharing?

I try to export a bitmap from my app using share intent without saving a file for a temporal location. All the examples I found are two-step
1) save to SD Card and create Uri for that file
2) start the intent with this Uri
Is it possible to make it without requiring WRITE_EXTERNAL_STORAGE permission, saving the file [and removing it afterwards]? How to address devices without ExternalStorage?
I had this same problem. I didn't want to have to ask for the read and write external storage permissions. Also, sometimes there are problems when phones don't have SD cards or the cards get unmounted.
The following method uses a ContentProvider called FileProvider. Technically, you are still saving the bitmap (in internal storage) prior to sharing, but you don't need to request any permissions. Also, every time you share the bitmap the image file gets overwritten. And since it is in the internal cache, it will be deleted when the user uninstalls the app. So in my opinion, it is just as good as not saving the image. This method is also more secure than saving it to external storage.
The documentation is pretty good (see the Further Reading below), but some parts are a little tricky. Here is a summary that worked for me.
Set up the FileProvider in the Manifest
<manifest>
...
<application>
...
<provider
android:name="androidx.core.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>
Replace com.example.myapp with your app package name.
Create res/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>
This tells the FileProvider where to get the files to share (using the cache directory in this case).
Save the image to internal storage
// 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 (IOException e) {
e.printStackTrace();
}
Share the image
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.myapp.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"));
}
Further reading
FileProvider
Storage Options - Internal Storage
Sharing Files
Saving Files
I try to export a bitmap from my app using share intent without saving a file for a temporal location.
In theory, this is possible. In practice, it is probably not possible.
In theory, all you need to share is a Uri that will resolve to the bitmap. The simplest approach is if that is a file that is directly accessible by the other application, such as on external storage.
To not write it to flash at all, you would need to implement your own ContentProvider, figure out how to implement openFile() to return your in-memory bitmap, and then pass a Uri representing that bitmap in the ACTION_SEND Intent. Since openFile() needs to return a ParcelFileDescriptor, I don't know how you would do that without an on-disk representation, but I have not spent much time searching.
Is it possible to make it without requiring WRITE_EXTERNAL_STORAGE permission, saving the file [and removing it afterwards]?
If you simply do not want it on external storage, you can go the ContentProvider route, using a file on internal storage. This sample project demonstrates a ContentProvider that serves up a PDF file via ACTION_VIEW to a PDF viewer on a device; the same approach could be used for ACTION_SEND.
If anyone still looking for easy and short solution without any storage permission (Supports nougat 7.0 as well). Here it is.
Add this in Manifest
<provider
android:name="android.support.v4.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>
Now create provider_paths.xml
<paths>
<external-path name="external_files" path="."/>
</paths>
Finally Add this method to your activity/fragment (rootView is the view you want share)
private void ShareIt(View rootView){
if (rootView != null && context != null && !context.isFinishing()) {
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(rootView.getDrawingCache());
if (bitmap != null ) {
//Save the image inside the APPLICTION folder
File mediaStorageDir = new File(AppContext.getInstance().getExternalCacheDir() + "Image.png");
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(mediaStorageDir));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (ObjectUtils.isNotNull(mediaStorageDir)) {
Uri imageUri = FileProvider.getUriForFile(getActivity(), getActivity().getApplicationContext().getPackageName() + ".provider", mediaStorageDir);
if (ObjectUtils.isNotNull(imageUri)) {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(waIntent, "Share with"));
}
}
}
}
}
Update:
As #Kathir mentioned in comments,
DrawingCache is deprecated from API 28+. Use below code to use Canvas instead.
Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), quality);
Canvas canvas = new Canvas(bitmap);
Drawable backgroundDrawable = view.getBackground();
if (backgroundDrawable != null) {
backgroundDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return bitmap;
This for sharing CardView as an Image then saving it in the cache subdirectory of the app's internal storage area.
hope it will be helpful.
#Override
public void onClick(View view) {
CardView.setDrawingCacheEnabled(true);
CardView.buildDrawingCache();
Bitmap bitmap = CardView.getDrawingCache();
try{
File file = new File(getContext().getCacheDir()+"/Image.png");
bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(file));
Uri uri = FileProvider.getUriForFile(getContext(),"com.mydomain.app", file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
getContext().startActivity(Intent.createChooser(shareIntent, "Share"));
}catch (FileNotFoundException e) {e.printStackTrace();}
}
});
Here is working method to make a screenshot of own app and share it as image via any messanger or email client.
To fix the bitmap not updating problem I improved Suragch's answer, using Gurupad Mamadapur's comment and added own modifications.
Here is code in Kotlin language:
private lateinit var myRootView:View // root view of activity
#SuppressLint("SimpleDateFormat")
private fun shareScreenshot() {
// We need date and time to be added to image name to make it unique every time, otherwise bitmap will not update
val sdf = SimpleDateFormat("yyyyMMdd_HHmmss")
val currentDateandTime = sdf.format(Date())
val imageName = "/image_$currentDateandTime.jpg"
// CREATE
myRootView = window.decorView.rootView
myRootView.isDrawingCacheEnabled = true
myRootView.buildDrawingCache(true) // maybe You dont need this
val bitmap = Bitmap.createBitmap(myRootView.drawingCache)
myRootView.isDrawingCacheEnabled = false
// SAVE
try {
File(this.cacheDir, "images").deleteRecursively() // delete old images
val cachePath = File(this.cacheDir, "images")
cachePath.mkdirs() // don't forget to make the directory
val stream = FileOutputStream("$cachePath$imageName")
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream) // can be png and any quality level
stream.close()
} catch (ex: Exception) {
Toast.makeText(this, ex.javaClass.canonicalName, Toast.LENGTH_LONG).show() // You can replace this with Log.e(...)
}
// SHARE
val imagePath = File(this.cacheDir, "images")
val newFile = File(imagePath, imageName)
val contentUri = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", newFile)
if (contentUri != null) {
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // temp permission for receiving app to read this file
shareIntent.type = "image/jpeg" // just assign type. we don't need to set data, otherwise intent will not work properly
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
startActivity(Intent.createChooser(shareIntent, "Choose app"))
}
}

Categories

Resources