Share Image to third party apps in android - android

I'm trying to share image with third party apps in android. I'm able to share image with all device except Xiaomi(Android 12) & OnePlus Node (Android 11).
val shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText)
shareIntent.putExtra(Intent.EXTRA_STREAM,imageUri)
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(Intent.createChooser(shareIntent, "Share using"))
Shows toast message --> this file is not supported.

val file = File(path, "${System.currentTimeMillis()}.png")
file.outputStream().use {
imageBitmap!!.compress(Bitmap.CompressFormat.PNG,100, it)
}
var imageUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".fileprovider",
file )

Related

How to open DCIM/Camera folder in default Files app from intent

I am making a camera app which takes both front camera and back camera images/video and I do not want individual thumbnails on the Camera preview for each file.
I want to open "/storage/emulated/0/DCIM/Camera" folder using the Files app and further open the photo/video which is not possible with ACTION_GET_CONTENT as it selects the image and exits the Files app as tried here -
val intent = Intent(Intent.ACTION_GET_CONTENT)
val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
intent.setDataAndType(uri, "*/*")
startActivity(Intent.createChooser(intent, "Open folder"))
I tried ACTION_VIEW too, but it is not specific to one folder and opens the gallery showing all media as tried here -
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.type = "image/*"
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
"image/*" shows images and videos too in the gallery for me which is good. When "*/*" is used we can use the Files app too but it opens the downloads folder.
One solution I found works only with ES Explorer as tried here -
val uri: Uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path.toString() + "/Camera")
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "resource/folder")
startActivity(intent)
This is due to "resource/folder" not supported leading to a crash. Changing "resource/folder" to "*/*" makes Files app open the downloads folder and Photos app to hang.
It seems gallery can do it via buckets, but it too is not universal.
I am not asking for much, just to display my Camera folder from where I can open and view any photo/video.
It was not possible if the user had not once selected a file using ACTION_GET_CONTENT was the opinion.
But now... try this code:
String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
// String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Uri uri = Uri.parse(scheme);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri); // Added in API level 26
startActivityForResult(intent, 12345);
Toast.makeText(context, "Picker opened in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();
It works here and i'm pretty amazed.
To open the Files app starting with a certain folder you can use below code but only for Android 11 devices sadly.
//String scheme = "content://com.android.externalstorage.documents/document/primary%3APictures";
//String scheme = "content://com.android.externalstorage.documents/document/primary%3ADownload";
//String scheme = "content://com.android.externalstorage.documents/document/10F9-2E19%3ADownload";
String scheme = "content://com.android.externalstorage.documents/document/primary%3ADCIM%2FCamera";
Uri uri = Uri.parse(scheme);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "vnd.android.document/root");
startActivity(intent );
Toast.makeText(context, "Files app opening in:\n\n" + uri.toString(), Toast.LENGTH_LONG).show();

How to share video by slack?

I made an implementation of share video and all works when I share video by WhatsUp. But I noticed that if I share video by Slack it doesn't play in Slack. It means I got the video file in my Slack account, click on it and it doesn't play... I can download this video and it will play, but in Slack it doesn't.
If I share a video from standard Gallery app to Slack it works...
So, what is the difference?
Here my sharing code
private fun openShareDialog(iC: Context, //
iPath: String) {
MediaScannerConnection.scanFile(//
iC.applicationContext, //
arrayOf(iPath), null //
) { _, iUri ->
var shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "video/*"
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "title")
shareIntent.putExtra(Intent.EXTRA_TITLE, "title")
shareIntent.putExtra(Intent.EXTRA_STREAM, iUri)
shareIntent = Intent.createChooser(shareIntent, iC.getString(R.string.tetavi_send_to))
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
iC.startActivity(shareIntent)
}
}
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
sharingIntent.putExtra(Intent.EXTRA_TITLE, "title");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, "share file with"));

Posting Multiple Images to Instagram using Intents

Instagram allows a single image or video to be uploaded programmatically from an Android app via Android Intents. I have been able to do this successfully. What I want to know is it possible for Instagram to handle multiple images using Intents? Not much to no information on this unfortunately. The following is my last attempt which opens Instagram briefly then closes with a toast message saying "Unable to load image".
Have tried both Intent.ACTION_SEND and Intent.ACTION_SEND_MULTIPLE
val fileUris = ArrayList<Uri>()
val newFile = File("/data/user/0/com.myapp.android/files/media/961087022.jpg")
val contentUri = getUriForFile(this, "com.myapp.fileprovider", newFile)
grantUriPermission("com.instagram.android", contentUri, FLAG_GRANT_READ_URI_PERMISSION)
fileUris.add(contentUri)
val newFile2 = File("/data/user/0/com.myapp.android/files/media/961146948.jpg")
val contentUri2 = getUriForFile(this, "com.myapp.fileprovider", newFile2)
grantUriPermission("com.instagram.android", contentUri2, FLAG_GRANT_READ_URI_PERMISSION)
fileUris.add(contentUri2)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/*"
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris)
shareIntent.`package` = "com.instagram.android"
startActivity(Intent.createChooser(shareIntent, "Share to"))
I was looking on how to share multiple video files to instagram stories.. I couldn't find how to do it reading the facebook documentation.
Instead i set the intent to send multiple files and set the intent package to "com.instagram.android", and surprisingly it worked..
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE) ;
share.setType("video/*");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
ArrayList<Uri> files = new ArrayList<Uri>();
for (String path : filesToSend) {
File myFiles = new File(path);
Uri doneUri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", myFiles);
files.add(doneUri);
}
share.setPackage("com.instagram.android");
share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(share);
Hope, this helps!!

share video to line app

I want to share video file to Line app from my app.
In android 6.0 and 7.0
I can use following code to share.
Uri uri = Uri.fromFile(fileFull);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("video/mp4");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, getString(R.string.sharevideoto)));
But the new android 8.0 need use fileprovider
Uri uri = FileProvider.getUriForFile(PlayvideoActivity.this,
BuildConfig.APPLICATION_ID + ".provider",fileFull);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("video/mp4");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, getString(R.string.sharevideoto)));
But Line app will show error when use fileprovider to share video.
But using fileprovider share picture is work.
for Kotlin
fun onShareVideo(uri: Uri, packageNameOfApp: String) {
val share = Intent(Intent.ACTION_SEND)
share.type = "video/*"
share.putExtra(Intent.EXTRA_STREAM, uri)
share.setPackage(packageNameOfApp) //change packageNameOfApp to jp.naver.line.android
startActivity(Intent.createChooser(share, "Share to"))
}
NOTE
You can use code above for twitter, instagram, gmail...
It worked good for all version.

Apps crashed when sending an image android ShareCompat and FileProvider

The relevant apps appear on screen to share to but when you select one of them, the selected app crashes. WhatsApp says "file format not supported".
File imageFile = new File(imagePath); // image path from mediastore
Uri contentUri = FileProvider.getUriForFile(
this,
"my.app.fileprovider",
imageFile);
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType(getContentResolver().getType(contentUri))
.setStream(contentUri)
.getIntent();
shareIntent.setData(contentUri);
shareIntent.setFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (shareIntent.resolveActivity(getPackageManager()) != null)
{
startActivity(Intent.createChooser(shareIntent, "Select App"));
}

Categories

Resources