views:

111

answers:

2

[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.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;    
            }
        }  

}

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")

A: 

why is the file expected to be _display_name in the exception?

Gary
Well :(( that was only a wrong attemp... because I don't really know how to tell the mail that it should add the file that had been clicked before :((
kivy
+1  A: 
Christopher
Thanks for the help..also for the corrections in my getView... and I will definitely take a look at CursorAdapter :) But my problem still is that I don't know how I can keep track of the video ID that is clicked on...
kivy
I added 'tagging' each view with the video URL to the `getView` example. So in `onItemClick`, you're passed a view `v`, which you can retrieve that tag from, using `getTag`.
Christopher
You can't keep changing the question; it's too overwhelming now and makes it hard to line up people's responses with your questions. You can always post further questions if you have individual issues that you can separate out.
Christopher
Sorry, didn't mean to confuse anyone...I should have deleted the old question and just posted a new one.
kivy
The tagging worked thank you, but I still got the problem that the uri that I get from the tag consists of /sdcard/filename and if I passed it on to attach the video to the mail it would become /sdcard/sdcard/filename ... how do I get rid of the second /sdcard
kivy
`Environment.getExternalStorageDirectory()` returns (usually) "/sdcard", so just remove that when creating the `File`.
Christopher
Yes, it does create the "/sdcard", so what you mean is getting entirely rid of 'Uri.fromFile(new File(Environment.getExternalStorageDirectory(),videocursor.getString(video_column_index))'??
kivy
Ok, I just found out that you can also create new File() only with a String, but I still don't know how to pass on the clicked item to i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(???)));
kivy
As I mentioned in the answer, retrieve the "tag" in `onItemClick` and store it in a temporary field. Then from your options menu action, pick up the URL from that temporary field.
Christopher