无法与Android应用程序共享音频文件

时间:2014-04-21 16:09:20

标签: android audio google-drive-api share

所以我有这个应用程序,当你按下按钮时,它允许你选择一个文件,并分享给任何允许共享的应用程序(最好是谷歌驱动器)

但是,我只能选择一个文件,并且应用程序选择不会出现,让我选择一个应用程序来发送文件。

此外,该文件也以.3gpp格式保存。

我的代码:

import java.util.List;
import android.os.Build;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {


     Button cameraButton;
     Button recordSoundButton;
     Button launchGallery;
     Button AudioViewer;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraButton = (Button)findViewById(R.id.cameraButton); //create buttons and check for ids
        cameraButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                takeAPhoto();
            }


        });

        recordSoundButton = (Button)findViewById(R.id.recordSoundButton);
        recordSoundButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                OpenUpRecordSound();
            }//end openRecord Sound


        });//end record sound button listener

        launchGallery = (Button)findViewById(R.id.PhotoViewer);
        launchGallery.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                LaunchUpAPhotoGallery();

            }


        });//end launch Gallery button listener

        AudioViewer = (Button)findViewById(R.id.AudioViewer);
        AudioViewer.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LaunchFileManager();


            }//end on click


        });//end audio viewer listener



     // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // For the main activity, make sure the app icon in the action bar
            // does not behave as a button
            ActionBar actionBar = getActionBar();
            actionBar.setHomeButtonEnabled(false);
        } //end if

    } //end onCreate

    public void OpenUpRecordSound(){

        startActivity(new Intent(MainActivity.this, RecordASound.class));

    }//end openupRecord sound

    public void takeAPhoto(){

        //fire up the camera app on the phone
        Intent i = new Intent();
        i.setAction(Intent.ACTION_CAMERA_BUTTON);
        i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CAMERA));
        sendOrderedBroadcast(i, null);

        // Ensure that there is a camera app on the phone!
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0);
        boolean isIntentSafe = activities.size() > 0;

        // Start an activity if it's safe
        if (isIntentSafe) {
            startActivity(i);
        }//end if

    }//end takeAPhoto

    public void LaunchUpAPhotoGallery(){

        Intent intent =  new Intent(Intent.ACTION_VIEW);
        intent.setType("image/*");
        startActivity(intent);

    }//end Photo launcher handler thingy

    public void LaunchFileManager(){

        Intent intent = new Intent();
         intent.setAction(Intent.ACTION_GET_CONTENT);
          intent.setType("file/*");
          startActivity(Intent.createChooser(intent, "Share the sound file"));


    }//end launch file manager



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

3 个答案:

答案 0 :(得分:1)

您需要在意图中实际指定文件Uri,例如:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(soundFile),"audio/*");
startActivity(Intent.createChooser(intent,"Share the sound file"));

答案 1 :(得分:1)

试试这个:

String filepath ="/sdcard/sample/sample.mp3";
                        Intent intent = new Intent();  
                        intent.setAction(Intent.ACTION_SEND);   
                        intent.setType("audio/*");
                        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filepath)));
                        startActivity(intent);

答案 2 :(得分:0)

String sharePath = Environment.getExternalStorageDirectory().getPath()
        + "/Soundboard/Ringtones/custom_ringtone.ogg";
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
相关问题