Android Sharing Intent cannot start

2019-06-09 04:20发布

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);

1条回答
成全新的幸福
2楼-- · 2019-06-09 05:07

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

查看更多
登录 后发表回答