将资产文件夹中的mp3文件共享到Whatsapp Android

时间:2015-01-06 08:40:28

标签: android eclipse mp3 assets whatsapp

我是Android的新手。

我已经尝试过这个论坛的所有帖子。但无法取得成功。 我正在尝试将资产文件夹中的mp3文件共享给whatsapp。

以下是我的代码。

这是我在主要活动中的代码:

package com.example.sharedemo;

import com.example.sharedemo.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    Button sharebutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharebutton = (Button) findViewById(R.id.sharebutton1);
        sharebutton.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Uri theUri = Uri.parse("content://com.example.sharedemo.Assetsprovider/gotShocked.mp3");    
                Intent theIntent = new Intent(Intent.ACTION_SEND);
                theIntent.setType("audio/mp3");
                theIntent.setPackage("com.whatsapp");
                theIntent.putExtra(Intent.EXTRA_STREAM,theUri);
                startActivity(Intent.createChooser(theIntent,"Share using"));
            }
        });
    }
}

ContentProvider中的代码:

package com.example.sharedemo;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class Assetsprovider extends ContentProvider {
     @Override
     public AssetFileDescriptor openAssetFile( Uri uri, String mode ) throws FileNotFoundException
     {

             AssetManager am = getContext( ).getAssets( );
            String file_name = uri.getLastPathSegment( );
            // String file_name = uri.getPath();

             if( file_name == null )
                     throw new FileNotFoundException( );
             AssetFileDescriptor afd = null;
             try
             {
                     afd = am.openFd(file_name);
             }
             catch(IOException e)
             {
                     e.printStackTrace( );
             }
             return afd;//super.openAssetFile(uri, mode);
     }
     @Override
     public String getType( Uri p1 )
     {
             // TODO: Implement this method
             return null;
     }

     @Override
     public int delete( Uri p1, String p2, String[] p3 )
     {
             // TODO: Implement this method
             return 0;
     }

     @Override
     public Cursor query( Uri p1, String[] p2, String p3, String[] p4, String p5 )
     {
             // TODO: Implement this method
             return null;
     }

    /* @Override
     public Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal )
     {
             // TODO: Implement this method
             return super.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal );
     }*/

     @Override
     public Uri insert( Uri p1, ContentValues p2 )
     {
             // TODO: Implement this method
             return null;
     }

     @Override
     public boolean onCreate( )
     {
             // TODO: Implement this method
             return false;
     }

     @Override
     public int update( Uri p1, ContentValues p2, String p3, String[] p4 )
     {
             // TODO: Implement this method
             return 0;
     }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sharedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name=".Assetsprovider"
            android:label="@string/app_name" > 
            </activity> 
         <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
        android:name="com.example.sharedemo.Assetsprovider"
        android:authorities="com.example.sharedemo.Assetsprovider"
        android:grantUriPermissions="true"
        android:exported="true" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

   Intent share = new Intent(Intent.ACTION_SEND);
   share.setType("audio/*");
   share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///"+mypath));
   startActivity(Intent.createChooser(share, "Share Sound File"));
   break;