Share image from Android App [duplicate] - java

This question already has answers here:
photo share intent in android
(3 answers)
Closed 6 years ago.
I'm having troubles sharing an image from my app to other apps.
The problem is when I share the image, it doesn't include the extension and other apps like messenger, whatsapp or gmail doesn't recognise them.
My code is:
Intent intent = new Intent(Intent.ACTION_SEND);
String path = "android.resource://com.xxxx.xxxxxxx/drawable/image";
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_TEXT, "Hello world!");
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share:"));
I'm having the same problem sharing audio:
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
String sharePath = "android.resource://com.xxx.xxxxx/raw/" + sound;
Uri uri = Uri.parse(sharePath);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("audio/mp3");
Intent i = Intent.createChooser(share, "Share Sound File");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
baseContext.startActivity(i);
I already checked the official documentation and still having problems: https://developer.android.com/training/sharing/send.html
Thanks for help or any hints!

You have to write the image or audio file to the sdcard or ExternalStorege before sharing. The reason is other apps don't have the access to the files which are in your app's private memory. So to share the image or any other file from your app, you must make it available for other apps to read.

Related

Share Image to Snapchat

I want to share image to snapchat but giving me "Sorry! You can only share images and videos"
I tried this code but doesnt work Android share image on snapchat
I have this intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setPackage("com.snapchat.android");
int n = Integer.parseInt(number.getText().toString());
Uri uri = Uri.fromFile(new File(db.getimage(n))); //This is retrieving the Uri path from DB
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Open Snapchat"));
the type that you are setting for the setType method is not correct,
if you want to support any format of Image, I suggest you first get the format of Image And after that pass the correct parameter to the setType() method.
for getting a file extension you can use this link:
link
For sharing data in snapchat, there is a kit available in their official website called Creative Kit.
Also you can find the Doc and Tutorials here.

How to share image (File Path) with content using share Intent in all devices

I am downloading image and taking it filepath to share but it is not working on all platforms except whatsapp. While it is showing can't upload image on insta and file not attached on gmail.
`private void genericShare(Activity activity) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, downloadedFilesPath);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_TEXT,getClipBoardText(activity));
activity.startActivity(Intent.createChooser(shareIntent,
activity.getResources().getText(R.string.share_using)));
}`
Check the following demo app code, after clone it just change the hardcoded file path.
https://github.com/nikkulshrestha/imageShare

Is there way to share *.json file via Intent or something else?

In my app I need to share *.json file to other app (messenger, google disk, etc). How can I do this via Intent or something else?
But when I trying to do this via Intent, I have some problems.
override fun shareBackupData(path: String) {
val uri = Uri.parse(path)
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type = "*/*"
startActivity(Intent.createChooser(shareIntent, "Choose"))
}
When I run this code, I choose app to share and then I see toast "unsupported attachment"
I had similar problems with this and I found this article where it recommends us to use FileProvider.
What it does is :
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.
I recommend you to take a look to the article and also if you want code, take a look at this Stackoverflow post
i think you can use the file as ExtrtaStream the following code is sharing image file you can change it to your json file
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "foo.jpg");//change it with your file
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using"));

How to open gallery in specified folder using intent

Can you help me how to open gallery in specified folder using intent?
It only opening gallery from specified folder like this (screenshot below).
However, it is not returning any value or activity.
I've try a lot of methods, but none open the specified gallery folder
I tried
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This works, but only opening gallery, not in specified folder
another one
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/CameraExample/");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));
this will open recent file, but it's not just viewing gallery, it choose a file, and return it.
Intent intent = new Intent();
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath() + "/CameraExample/");
intent.setDataAndType(uri, "*/*");
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
and it only shows a whitescreen. Is it possible to open gallery in specified folder? It would open like this if possible:
is it possible to open gallery in specified folder?
No, sorry.
The gallery selection was not designed to be opened from any location like that. Supported locations:
Album location(
/sdcard/dcim)

Universal way to share images to other apps

I'm developing an app which can send an image to other apps. After Nougat was released we should use FileProvider. But I noticed that this approach is not universal. For earlier versions of Android we should use Uri.getUriFromFile() method. But using FileProvider also doesn't work on Nougat for some apps (i.e. default messages app). Is there any universal method which will work with all apps, or I should define apps which can't work with FileProvider only on practice?
Following code must work properly:
File file = new File(filePath);
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.fileprovider", file);
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, "Share image");
}

Categories