Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java VideoView无法从Internet存储打开内容_Java_Android_Video_Local Storage_Android Videoview - Fatal编程技术网

Java VideoView无法从Internet存储打开内容

Java VideoView无法从Internet存储打开内容,java,android,video,local-storage,android-videoview,Java,Android,Video,Local Storage,Android Videoview,我有以下代码: try { myVideoView.setMediaController(mediaControls); File mydir = getDir("myDir", Context.MODE_PRIVATE); myVideoView.setVideoPath(mydir.getPath()+"/fileName.3gp"); } catch (Exception e) { Log.e("Error", e.g

我有以下代码:

try {
        myVideoView.setMediaController(mediaControls);
        File mydir = getDir("myDir", Context.MODE_PRIVATE);
        myVideoView.setVideoPath(mydir.getPath()+"/fileName.3gp");
    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    myVideoView.requestFocus();
    myVideoView.setOnPreparedListener(new OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            progressDialog.dismiss();
            myVideoView.seekTo(position);
            if (position == 0) {
                myVideoView.start();
            } else {
                myVideoView.pause();
            }
        }
    });

在onCreate()方法中。问题是它抛出了一个IOException,并表示
无法打开内容
。如果我使用
myVideoView.setVideoURI(path/to/server)
而不是
myVideoView.setVideoPath(path/to/local/storage)
,它的效果会非常好。
Manifest.xml
上的所有权限都很好,并且没有说明找不到该文件或其他内容。它说无法打开它。这也不是一个视频格式问题,因为当我从服务器播放它时,它是相同的视频。如果这是一个有用的信息,我在后台有另一个活动(同一个应用程序),而VideoView活动在前台。谢谢大家!

嗨,使用它播放内部视频

public class MainActivity extends Activity {
private Cursor videocursor;
private int video_column_index;
ListView videolist;
int count;
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init_phone_video_grid();
}

@SuppressWarnings("deprecation")
private void init_phone_video_grid() {
    System.gc();
    String[] proj = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE };
    videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
    count = videocursor.getCount();
    videolist = (ListView) findViewById(R.id.PhoneVideoList);
    videolist.setAdapter(new VideoAdapter(getApplicationContext()));
    videolist.setOnItemClickListener(videogridlistener);
}

private OnItemClickListener videogridlistener = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        System.gc();
        video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        videocursor.moveToPosition(position);
        String filename = videocursor.getString(video_column_index);
        Intent intent = new Intent(MainActivity.this, ViewVideo.class);
        intent.putExtra("videofilename", filename);
        startActivity(intent);
    }
};

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 position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.gc();
        ViewHolder holder;
        String id = null;
        convertView = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(vContext).inflate(R.layout.listitem, parent, false);
            holder = new ViewHolder();
            holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
            holder.txtSize = (TextView) convertView.findViewById(R.id.txtSize);
            holder.thumbImage = (ImageView) convertView.findViewById(R.id.imgIcon);

            video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
            videocursor.moveToPosition(position);
            id = videocursor.getString(video_column_index);
            video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
            videocursor.moveToPosition(position);
            // id += " Size(KB):" +
            // videocursor.getString(video_column_index);
            holder.txtTitle.setText(id);
            holder.txtSize.setText(" Size(KB):" + videocursor.getString(video_column_index));

            String[] proj = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA };
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Video.Media.DISPLAY_NAME + "=?",
                    new String[] { id }, null);
            cursor.moveToFirst();
            long ids = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));

            ContentResolver crThumb = getContentResolver();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND, options);
            holder.thumbImage.setImageBitmap(curThumb);
            curThumb = null;

        } /*
         * else holder = (ViewHolder) convertView.getTag();
         */
        return convertView;
    }
}

static class ViewHolder {

    TextView txtTitle;
    TextView txtSize;
    ImageView thumbImage;
}
}

视频观看课在这里

public class ViewVideo extends Activity {
  private String filename;
  VideoView vv;
  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.gc();
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        filename = extras.getString("videofilename");
        // vv = new VideoView(getApplicationContext());
        setContentView(R.layout.activity_main);
        vv = (VideoView) findViewById(R.id.videoView);
        vv.setVideoPath(filename);
        vv.setMediaController(new MediaController(this));
        vv.requestFocus();
        vv.start();
  }
}

Xml部分在这里

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

         <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="#242425"
        android:gravity="center"
        android:orientation="vertical" >

            <VideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    </LinearLayout>
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5" >

       <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@android:color/white"
        android:gravity="center"
        android:orientation="vertical" >

        <ListView 
            android:id="@+id/PhoneVideoList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@android:color/darker_gray"
            android:cacheColorHint="#00000000"
            android:dividerHeight="2dp"></ListView>
     </LinearLayout>
    </LinearLayout>
    </LinearLayout>