[更新2]:您好,
在尝试将视频附加到电子邮件后,我想(虽然不确定!)我更接近目标,但我发现了两个问题......
问题1:FILENAME不能正常显示:我试图干的是我点击的视频名称,看看我怎么能得到这个名字。这就是我所做的......
vGrid.setOnItemClickListener(new OnItemClickListener() {
@Override // click on item and open options menu
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String [] proj={MediaStore.Video.Media.DATA};
videocursor = managedQuery( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null,null,null);
video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videocursor.moveToPosition((int) vGrid.getSelectedItemId());
// And here we get the filename
String filename = videocursor.getString(video_column_index);
Toast.makeText(ShareGalleryView.this, filename, Toast.LENGTH_SHORT).show();
openOptionsMenu(); //Opens Options Menu by clicking on an item
}
});
...但不是只显示文件名我得到sdcard / filename.mp4,所以我的第一个问题是如何摆脱“sdcard /”部分,因为如果我使用
中的 videocursor.getString(video_column_index)
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),videocursor.getString(video_column_index))));
用于将视频附加到邮件,我得到以下Logcat输出:
07-18 18:53:47.518: ERROR/Mms/media(179): java.io.FileNotFoundException: /sdcard/sdcard/Video0004.mp4
那是问题1 ......现在问题2
[问题2] 我还发现无论在哪个按钮上我点击文件名输出总是一样的,只显示“sdcard / Video0004.mp4”,所以当我点击Video0010时它还显示“sdcard / Video0004.mp4”,因此我的第二个问题是如何在我的代码中确保显示所点击的项目并附加到电子邮件并始终是第一个视频。
请,请帮助我 ......这真让我感到困惑......我现在没有想法,如何解决这些问题。
我将在下面发布完整的代码......谢谢
package com.mobilevideoeditor.moved;
import java.io.File;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ShareGalleryView extends Activity {
private Cursor videocursor;
private int video_column_index;
GridView vGrid;
int count;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videogrid);
//create new Grid View
vGrid=(GridView) findViewById(R.id.vgrid);
registerForContextMenu(vGrid);
vGrid.setAdapter(new VideoAdapter(this));
init_phone_video_grid();
vGrid.setOnItemClickListener(new OnItemClickListener() {
@Override // click on item and open options menu
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String [] proj={MediaStore.Video.Media.DATA};
videocursor = managedQuery( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null,null,null);
video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videocursor.moveToPosition((int) vGrid.getSelectedItemId());
// And here we get the filename
String filename = videocursor.getString(video_column_index);
Toast.makeText(ShareGalleryView.this, filename, Toast.LENGTH_SHORT).show();
openOptionsMenu(); //Opens Options Menu by clicking on an item
}
});
}
private void init_phone_video_grid() {
System.gc();
String[] proj = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DATA
};
videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
count = videocursor.getCount();
GridView vGrid=(GridView) findViewById(R.id.vgrid);
vGrid.setAdapter(new VideoAdapter(this));
}
@Override //creates options menu with menu-items
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_gallery_share, menu);
return super.onCreateOptionsMenu(menu);
}
@Override //what happens when a menu item is clicked
public boolean onOptionsItemSelected (MenuItem item){
try{
//Facebook
if (item.getItemId() == R.id.menu_facebook)
{
//TODO open fb
new AlertDialog.Builder(this)
.setTitle("No Service")
.setMessage("Sorry, Facebook is not supported yet!")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}}).show();
return true;
}
//YouTube
else if (item.getItemId() == R.id.menu_youtube)
{
//TODO open YouTube
new AlertDialog.Builder(this)
.setTitle("No Service")
.setMessage("Sorry, YouTube is not supported yet!")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}}).show();
return true;
}
else if (item.getItemId() == R.id.menu_email)
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("video/mp4");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),videocursor.getString(video_column_index))));
startActivity(i);
return true;
}
else if (item.getItemId() == R.id.menu_bluetooth)
{
// TODO send via bluetooth
new AlertDialog.Builder(this)
.setTitle("No Service")
.setMessage("Sorry, Bluetooth is not supported yet!")
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}}).show();
return true;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return super.onContextItemSelected(item);
}
public class VideoAdapter extends BaseAdapter {
private Context vContext;
public VideoAdapter(Context c) {
vContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(vContext.getApplicationContext());
String id = null;
if (convertView == null) {
video_column_index =
videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(position);
id = videocursor.getString(video_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
大家好,
我正在制作视频应用,您可以在其中点击SD卡中的视频。此点击事件会打开一个菜单(我使用了一个选项菜单),该菜单为用户提供了不同的共享选项,例如电子邮件,蓝牙等。这部分到目前为止工作正常...我现在要做的是,当用户选择“电子邮件”时,应用程序应通过意图打开手机的电子邮件应用程序(这也可以正常工作)应该直接将他之前点击的视频附加到新电子邮件中。
[更新] 最后一部分是,我被卡住了,因为我不知道如何获取被点击以打开图片的图片文件名。这是我的代码中的一部分,我遇到了问题(参见 “不知道该怎么做” )
答案 0 :(得分:1)
您没有跟踪点击的视频ID。
完成后,您可以(大概)通过查询ContentProvider
(就像您在VideoAdapter
中所做的那样)来获取视频的路径。或者,更简单 - 因为您已经查询了MediaStore.Video.Media.DATA
- 请使用convertView.setTag()
将该网址附加到视图。
您可以在onItemClick
调用期间将其存储在本地变量中 - 通过调用String uri = (String) v.getTag()
可以直接使用URI。
进一步说明:
System.gc()
,尤其不要使用getView()
方式。这将导致大量不必要的减速。vContext.getApplicationContext()
;只需使用vContext
。getView
应该更像下面的内容:private static int displayNameIndex = -1;
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
// Need to create a new view
tv = new TextView(context);
} else {
// Otherwise, we can recycle the one given to us
tv = (TextView) convertView;
}
// Bind cursor data to UI
videocursor.moveToPosition(position);
id = videocursor.getString(1); // column index of Media.DISPLAY_NAME
tv.setText(id);
// Bundle video URI into the view
String videoUri = videocursor.getString(2); // column index of Media.DATA
tv.setTag(videoUri);
return tv;
}
我也会看看CursorAdapter,而不是自己做游标(将光标移动到正确的位置等)。