[UPDATE 2]: Hi there again,
after still trying to attach a video to an email, I think (not sure though!) that I am a little closer to my goal, but I discovered two problems...
PROBLEM 1: FILENAME DOESN'T SHOW PROPERLY: what I was trying was to toast the name of the video I clicked on to see how I can get the name. This is what I have done...
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
}
});
...but instead of only displaying the filename I get sdcard/filename.mp4, so my first question is how to get rid of the "sdcard/" part because if I use
videocursor.getString(video_column_index)
in
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),videocursor.getString(video_column_index))));
for attaching the video to the mail, I get the following Logcat output:
07-18 18:53:47.518: ERROR/Mms/media(179): java.io.FileNotFoundException: /sdcard/sdcard/Video0004.mp4
That was problem 1...now Problem 2
[PROBLEM 2] I also discovered that no matter on which button I click the filename output is always the same and only shows the "sdcard/Video0004.mp4", so when I click on Video0010 it also shows "sdcard/Video0004.mp4", thus my second problem is how do I make sure in my code that the item that was clicked on is shown and also attached to the email and always the first video.
Please, please, help me...This is really confusing me...and I have no Idea right now, how to solve these problems.
I will post the entire code below...Thank you
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.setOnItemClic开发者_StackOverflow中文版kListener(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;
}
}
}
Hi everyone,
I am working on a Video App, where you can click on a video from the sdcard. This click event opens a menu (I used a options menu) that provides the user with different sharing options, e.g. email, bluetooth etc. This part works fine so far... What I am now trying to do is, when the user chooses "email" the app should open the email app of the phone via an intent (this also works fine) and should directly attach the video he clicked before to the new email .
[UPDATE] The last part is, where I am stuck because I don't know how to get the image filename that was clicked to open the image. This is the part in my code, where I got the problem (see "DON'T KNOW WHAT TO PUT HERE")
You're not keeping track of which video ID is being clicked on.
Once you have that, you can (presumably) get the path to the video by querying the ContentProvider
(like you do in VideoAdapter
). Or, simpler — since you're already querying for MediaStore.Video.Media.DATA
— attach the URL to the view with convertView.setTag()
.
You could store this in a local variable during the onItemClick
call — the URI should be available directly by calling String uri = (String) v.getTag()
.
Some further notes:
- Don't call
System.gc()
, especially not in agetView()
method. That will cause a huge number of unnecessary slowdowns. - Don't call
vContext.getApplicationContext()
; just usevContext
. - You're not recycling views properly. Your
getView
should look more like below:
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;
}
I would also look at CursorAdapter, rather than having to do cursor stuff yourself (moving the cursor to the right position etc.).
精彩评论