我使用以下代码在audio files
共享菜单中注册我的应用程序:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
</intent-filter>
如何在我的活动中获取文件名?
我测试了下面的代码,但没有工作:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String action = intent.getAction();
String FilePath = "";
if (Intent.ACTION_SEND.equals(action)) {
if (extras.containsKey(Intent.EXTRA_STREAM)) {
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
FilePath = parseUriToFilename(uri);
}
}
}
public String parseUriToFilename(Uri uri) {
String selectedFile = null;
String filemanagerPath = uri.getPath();
String[] projection = { MediaStore.Audio.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
selectedFile = cursor.getString(column_index);
}
if (selectedFile != null) {
return selectedFile;
}
else if (filemanagerPath != null) {
return filemanagerPath;
}
return null;
}