真的需要帮助 - 如何将视频附加到电子邮件? [2个新问题]

发布于 2024-09-10 05:01:10 字数 8613 浏览 1 评论 0原文

[更新2]:您好,

在仍然尝试将视频附加到电子邮件后,我认为(虽然不确定!)我离我的目标更近了一点,但我发现了两个问题...

问题 1:文件名无法正确显示:我试图为我点击的视频的名称祝酒,看看如何获​​取该名称。这就是我所做的...

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 卡中的视频。此点击事件打开一个菜单(我使用了选项菜单),为用户提供不同的共享选项,例如电子邮件、蓝牙等。到目前为止,这部分工作正常...我现在想做的是,当用户选择时“电子邮件”应用程序应该通过意图打开手机的电子邮件应用程序(这也可以正常工作),并且应该直接将他之前单击的视频附加到新电子邮件中。

[更新] 最后一部分是,我陷入困境,因为我不知道如何获取单击打开图像的图像文件名。这是我的代码中出现问题的部分(请参阅“不知道要在这里放置什么”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-09-17 05:01:10

您没有跟踪哪个视频 ID 被点击。

获得该信息后,您(大概)可以通过查询 ContentProvider 获取视频的路径(就像在 VideoAdapter 中所做的那样)。或者,更简单 - 因为您已经在查询 MediaStore.Video.Media.DATA - 使用 convertView.setTag() 将 URL 附加到视图。

您可以在 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,而不必自己执行光标操作(将光标移动到正确的位置等)。

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 a getView() method. That will cause a huge number of unnecessary slowdowns.
  • Don't call vContext.getApplicationContext(); just use vContext.
  • 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.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文