嗨所有我是Android的新手,我尝试从Access Folder Android访问图像它的工作正常。以下是代码。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_picturedetail, container, false);
Bundle b = getArguments();
Drawable d;
am = getActivity().getAssets();
PictDictDetails pd = (PictDictDetails) b.getSerializable("pictobj");
ImageView iv = (ImageView)v.findViewById(R.id.iv_picture);
TextView tv = (TextView)v.findViewById(R.id.tv_picture_name);
String path = pd.getPath();
String name = pd.getName();
try
{
InputStream ims = am.open(path);
d = Drawable.createFromStream(ims, null);
iv.setImageDrawable(d);
}
catch(IOException e)
{
e.printStackTrace();
}
}
现在我正在尝试使用完全相同的逻辑BUt在VideoView中播放视频它无法正常工作 请找到以下代码,并帮助我在哪里错了? 感谢您的帮助。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.videofragment, container, false);
Bundle b = getArguments();
// Drawable d;
am = getActivity().getAssets();
PictDictDetails pd = (PictDictDetails) b.getSerializable("pictobj");
VideoView iv = (VideoView)v.findViewById(R.id.iv_video);
TextView tv = (TextView)v.findViewById(R.id.tv_picture_name);
String path = pd.getPath();
String name = pd.getName();
SurfaceHolder holder = iv.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);
//AssetFileDescriptor afd;
try
{
//InputStream ims = am.open(path);
// d = Drawable.createFromStream(ims, null);
//iv.setImageDrawable(d);
// AssetFileDescriptor descriptor = am.openFd(path);
// long start = descriptor.getStartOffset();
// long end = descriptor.getLength();
/// mediaPlayer.setDataSource(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getLength());
// MediaController mediaController= new MediaController();
// mediaController.setAnchorView(iv);
// iv.setMediaController(mediaController);
AssetFileDescriptor afd = am.openFd(path);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
答案 0 :(得分:0)
注意:
1)您必须将视频复制到项目的res / raw文件夹中,而不是资产。
2)支持的格式(3gp,wmv,mp4),并在文件名中以小写,数字,下划线和点命名: video_file.mp4
VideoView videoView = (VideoView)this.findViewById(R.id.videoView)
String uri = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
videoView.setVideoURI(Uri.parse(uri));
videoView.start();
您可以使用活动助手方法getPackageName()
,在构建视频的正确URI时,代码可以使用该方法。