I am new in android Java. I am trying to share an image in android application using Phonegap. I get new class "Share" as CordovaPlugin, code is follow...
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri uri = Uri.parse("file:///android_asset/www/sharethis.png");
share.putExtra(Intent.EXTRA_STREAM, uri);
Above code is not working, it showing like this ...
is think i cant get Exact image file location. My file location
I tried bellow code also, But not work,
String imagePath = Environment.getExternalStorageDirectory()
+ "/assets/www/sharethis.png";
Please Help
Android asset folder is private to your app so you can't share files from there directly. You need to copy the file from assets to a public directory in the filesystem and then send the share intent pointing to the public file. Have a look here
Related
I have an app that downloads files to folder selected by the user.
I get access to the folder using :
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent,REQUEST_PICK_FOLDER_TO_DOWNLOAD);
create the downloaded file with
DocumentFile newFile = destDir.createFile(mime, file.getFileName());
I need to share the file. For example if this is a text file, pdf or word doc user can open it in the appropriate app.
File URI seems pretty strange:
"content://com.android.externalstorage.documents/tree/primary%3Amy_logs/document/primary%3Amy_logs%2Fford-f-150-raptor-2019-491746%20(1).jpg"
But it is a file created with createFile and I am able to write the contents.
I am not sure FileProvider can help as the dest folder is any folder on the device.
I am not sure is there anyway to share that URI with any other app?
Thank you very much!
V
I'm trying to share Google File Type (eg: application/vnd.google-apps.document, application/vnd.google-apps.form, application/vnd.google-apps.presentation, application/vnd.google-apps.spreadsheet) with the Intent.
My code:
private void shareFile(String uri) {
ContentResolver contentResolve = getContentResolver();
String mimeType = contentResolve.getType(Uri.parse(uri));
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse(uri));
intentShareFile.setType(mimeType);
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Hey, check out this file!");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
But when I click an apps to share that file, it doesn't respond and not do anything. Even when I'm trying to upload it on Google Drive, it says that upload is unsuccessful. The share file works fine when I share pdf or docx file.
Apparently, we can't share the Google File Type via Intent. We have to convert the file that has Google File Type to something like Microsoft File Type (application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/msword, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/x-excel, application/vnd.ms-powerpoint, or application/vnd.openxmlformats-officedocument.presentationml.presentation).
Is there any way to convert the URI mimeType so I can share a file that has Google File Type? For the example is changing a file that has application/vnd.google-apps.document as its mimeType to application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation, and etc. Is there a way to do that so I can share a file that has Google File Type? Any help will be appreciated, thanks!
Do it like this:
Intent uploadIntent = ShareCompat.IntentBuilder.from(this)
.setType(mimeType)
.setStream(fileUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(uploadIntent)
Please note setStream requires Uri.
I am new to android development.
I am trying to open the default gallery app to view an image like this:
Uri u = Uri.parse((String) gridView.getAdapter().getItem(position);
Intent i = new Intent();
i.setDataAndType(u, "image/*");
i.setAction(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
I provide the file paths to my images like this:
/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20191023-WA0045.jpg
/storage/emulated/0/Pictures/9GAG/1565987862000.jpg
My App and Google Photos can display the image given this path, the Samsung Gallery( tested on Android Oreo) or the Xiaomi Gallery App (tested on Android X) can't find the image; also if i try to add "content://" or "file://" at the beginning.
I suspect that the apps want to have a Content URI or some kind of symbolic link without the "/storage/emulated/0/" part instead of a File URI. I searched for methods to get the content URI out of the file path, but didn't find any examples for Android X.
So what is the correct URI to pass on through the Intent and how do i get it?
Or maybe somebody knows a better way to just open the gallery app to a specific picture i provide by an absolute file path (Both External Storage and SD Card)?
Thank you in advance and greetings!
copied from this question here , this might work for you
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
I want to share the audio file in android using but didn't found any working solution.
In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).
Also checked that it is creating the file correctly.
I tried to share the audio file with below code;
File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
But while sharing and selecting any app it show unable to attach File.
Also Used FileProvider Class for Nougat Devices but it didn't worked as well.
Checked multiple SO Link as well:
Sharing an audio file
Picking up an audio file android
With regards to the code that you have:
Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri
Use audio/wav, not audio/*, for the MIME type
Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.
I am new in android Java.
I am trying to share an image in android application using Phonegap. I get new class "Share" as CordovaPlugin, code is follow...
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
Uri uri = Uri.parse("file:///android_asset/www/sharethis.png");
share.putExtra(Intent.EXTRA_STREAM, uri);
cordova.getActivity().startActivityForResult(share, 0);
Above code is not working, it showing like this ...
is think i cant get Exact image file location.
My file location
I tried bellow code also, But not work,
String imagePath = Environment.getExternalStorageDirectory()
+ "/assets/www/sharethis.png";
Android asset folder is private to your app so you can't share files from there directly. You need to copy the file from assets to a public directory in the filesystem and then send the share intent pointing to the public file