I am writing an Android application to upload a mp4 format video from sdcard to youtube using sharing Intent. It ran perfectly at the first time when I installed to my phone. However, after I restart the app, I cannot share it again. If i share to whatsapp, "sharing fail" apear. If I click youtube, I got "no media uri(s)" message on logcat. Reinstalling on same phone or restarting the phone cannot solve the problem.
Even when I install the app in same code to other android device, it only runs perfectly once. When i restart the app, same problem happened.
I got the code from this website:
Trouble with youtube upload
And this is my code:
findViewById(R.id.button2).setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("video/*");
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, Environment.getExternalStorageDirectory().getAbsolutePath()+"/myVideo/myVideoTemp.mp4");
ContentResolver resolver = getBaseContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share video"));
}
});
p.s. there is no error in red color on logcat
Problem solved after adding sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Try the following to invoke the "share" applications:
//Use the URI that points to the video file, in my case, the video file is in local storage (sd card)
Uri fileUri = Uri.fromFile(videoFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
//Use the setDataAndType method to provide the URI and MIME type to the file
//Please note, you need to use setDataAndType method to make it work
intent.setDataAndType(fileUri,URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
I have a blog to show how you can invoke video players from your app with a video file stored locally. It may help:
http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player
Related
I'm using this code for some years now and it worked fine:
final Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.setType("audio/mpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM,
SoundProvider.getUriForSound(getContext(), sound));
getActivity()
.startActivity(Intent.createChooser(sharingIntent,
getContext().getString(R.string.share)));
My SoundProvider generates a URI that starts with content:// which is picked up by a FileProvider (actually the same SoundProvider). This provider reads an audio file from my raw folder.
The sounds was playable directly in WhatsApp (and not a generic file) and shown with the correct title from the ID3 tags.
This has worked flawlessly and still does with Telegram/Dropbox etc. but up until a recent WhatsApp update from a few months ago it fails with the message "Sharing failed please try again".
Is anyone aware of any changes made by WhatsApp and has encountered something similar?
Try this:
Uri uri = Uri.parse(audioPath);
Intent shareIntent = new Intent();
shareIntent.setType("audio/*");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
I had to work around this by copying the sounds to the external-files-dir.
I don't know why whatsapp suddenly doesn't accept files from the raw directory served by a FileProvider anymore while other apps still do without any problems.
I am trying to save a 10sec long mp4 file in the gallery (the goal is to upload the vid to facebook).
The log says to me that the file duration is 10sec long - as he should be, but when I'm trying to upload it to Facebook (manually), it appears like the video is 0sec long and I can't upload it.
(if I'm watching the video on the phone - its works normally, just when I trying to upload it t FB its happens)
private void addVideoGallery( File file ) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.VideoColumns.DURATION,10000);
values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
Log.d("dur",""+values.getAsLong(MediaStore.Video.VideoColumns.DURATION));
getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
I just want to know if I'm setting the duration right (the actual duration is fixed on 10sec), or I should change/update anything?
I've found the solution, i just used the MediaScanner like this:
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
For further information: http://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/
[EDIT] I'm working on Android Lollipop. I want to share (upload) my video (.mp4) from sdcard to Facebook, Youtube,... using intent SEND. My problem is that: when I upload video first time, everything works perfectly. But if I try to upload this video again, nothing is attached. (For example: first time: I upload video to Facebook successfully. Then I try to upload it to Facebook again, video is not attached). Here is my code now:
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.TITLE, "My Test");
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, "/sdcard/rec5.mp4");
ContentResolver resolver = getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,content);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
startActivity(Intent.createChooser(intent, "Share using"));
Can anybody tell me what did I miss in my code?
I am currently using a photo gallery I built through GridLayout but this seems to use a lot of memory (OOME coming up) when dealing with a lot of Bitmaps. Therefore, I am trying to use the Intent.ACTION_GET_CONTENT to open photos on the device and select photos through there.
The camera function works correctly as it takes the photo, returns to the onActivityResult function, and updates it to my Gridlayout photo gallery.
I am currently on a Nexus 7 running Android 4.4 (KitKat)
[PROBLEMS]
When I try to view photos through Intent.ACTION_GET_CONTENT, none of the new photos I just took through the application are there.
Running Intent.ACTION_GET_CONTENT to filter results from a specific directory and file type doesn't seem to work.
[Code information]
MainForm is a (class)
photoPath = new File(directory, photoName)
directory is a (File) set to my current working directory
photoName is a (String)
CAMERA_CODE is an (int)
[MY CODE]
Here is my code for launching the camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cUri = Uri.fromFile(photoPath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cUri);
startActivityForResult(intent, CAMERA_CODE); // launch camera
[What I tried + Errors for Updating Photo Gallery library]
MediaScannerConnection
I tried to use this to update the photolist and then launch the intent, but it did not work (I may be using this incorrectly, but it was suggested in this post)
MediaScannerConnection.scanFile(MainForm.this, new String[] { directory.getAbsolutePath() },null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setData(uri);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 12345);
}
});
getContentResolver()
I read in this question to try using this method, but it doesn't seem to work either
MainForm.this.getContentResolver().notifyChange(MediaStore.Images.Media.INTERNAL_CONTENT_URI, null);
sendBroadcast
In this question and here to re-index photos on the device, the answer suggested using sendBroadcast
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(directory)));
but I was greeted with this error
E/AndroidRuntime(30177): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=30177, uid=10122
What does this error mean?
[What I tried + Errors for ACTION_GET_CONTENT intent]
No matter what I put under intent.setData, opening the intent doesn't seem to open that folder, but instead it opens the android browser under the tab "Recent"
I followed this question
ANY TIPS OR SUGGESTIONS WOULD BE GREATLY APPRECIATED! If there are any other codes or log results I should post, please comment below!
We've been trying to solve this issue for the past few hours, finally decided we should revert to StackOverflow.
Here goes -
We have an application that downloads a PDF file from a server to the app's cache directory and then opens it using the packet manager. Of course it goes through grantUriPermission() to give read (and write) permissions to all available packages.
Though it works great on most devices, today we encountered a device that has POLARIS Office 5 installed as the default PDF viewer.
Whenever we're opening the file, Polaris simply displays a message saying "This document cannot be opened".
I should say that when trying to open the file through Acrobat Reader, it works great.
Also, when we copied the file from the cache directory to an external directory (using Android's File manager) and then opened it in Polaris manually it worked great.
We would have given up on it, but since Polaris is the default viewer on many devices, we really would love solving that problem.
Here is the code -
public void onDownloadDone(String filepath) {
// Set a file object that represents the downloaded file
File file = new File(filepath);
// Set an intent for the external app
Intent intent = new Intent(Intent.ACTION_VIEW);
// Get mime type of the downloaded file
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(filepath);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
intent.setType(mimeType);
// Look for installed packges according to the file's mime type
PackageManager pm = context.getPackageManager();
Uri contentUri = FileProvider.getUriForFile(context, "myapp.fileprovider", file);
// Set the file uri in the intent
intent.setData(contentUri);
// Give permissions to the file to each external app that can open the file
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo externalApp: activities){
String packageName = externalApp.activityInfo.packageName;
context.grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
// Start the activities (or launch the menu) if any activity exists
if (activities.size() > 0){
context.startActivity(intent);
} else {
System.out.println("Warning!!! No app for file " + filepath);
}
}
Thank a lot!
I had exactly the same problem. The PDF files would open with ezPDF and Adobe but not with Polaris Viewer.
You have to set the data and type with:
intent.setDataAndType(uri, "application/pdf");
instead of using:
intent.setType("application/pdf");
intent.setData(uri);
For me the following is working fine with Polaris now:
Uri uri = FileProvider.getUriForFile(MyApplication.getContext(), MyApplication.getContext().getPackageName() + ".myfileprovider", destFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);