我正在制作一个简单的应用来播放视频。我这样做是为了了解textView。我已经完成了所有,但是当我重现视频应用程序崩溃时。我在这里发布所有代码。 这是MainActivity,是应用程序的唯一活动。
package com.example.movieplayer;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class MoviePlayerActivity extends Activity
implements OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_player);
ArrayList<Movie> movieList = new ArrayList<Movie>();
// elenco campi per la query sui media
String[] mediaColumns = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.MIME_TYPE
};
// elenco campi per la query sui thumbnail
String[] thumbnailColumns = { MediaStore.Video.Thumbnails.DATA };
// cursore sulle righe risultato
Cursor cursoreMedia = getContentResolver().
query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
mediaColumns, null, null, null);
// per ogni riga del risultto
if ((cursoreMedia != null) && cursoreMedia.moveToFirst()) {
do {
int id = cursoreMedia.getInt(cursoreMedia
.getColumnIndex(MediaStore.Video.Media._ID));
Cursor cursoreMiniature = getContentResolver().
query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbnailColumns, MediaStore.Video.Thumbnails.VIDEO_ID
+ "=" + id, null, null);
// nuovo oggetto per l'arraylist
Movie movie = new Movie(cursoreMedia, cursoreMiniature);
movieList.add(movie);
} while (cursoreMedia.moveToNext());
}
MovieListAdapter movieListAdapter = new MovieListAdapter(this, movieList);
ListView movieListView = (ListView)findViewById(R.id.listView1);
movieListView.setAdapter(movieListAdapter);
movieListView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Movie movie = (Movie) parent.getAdapter().getItem(position);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(movie.getMovieFilePath()),
movie.getMimeType());
startActivity(intent);
}
}
这是电影课。
package com.example.movieplayer;
import android.database.Cursor;
import android.provider.MediaStore;
class Movie {
private String titolo;
private String videoFilePath;
private String mimeType;
private long durata;
private String miniaturaFilePath;
public Movie(Cursor mediaCursor, Cursor thumbnailCursor)
{
titolo = mediaCursor.getString(mediaCursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
videoFilePath = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Media.DATA));
mimeType = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
durata = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Video.Media.DURATION));
if ((thumbnailCursor != null) && thumbnailCursor.moveToFirst())
miniaturaFilePath = thumbnailCursor.getString(thumbnailCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
else
miniaturaFilePath = null;
}
public String getTitolo() {
return titolo;
}
public String getMovieFilePath() {
return videoFilePath;
}
public String getMimeType() {
return mimeType;
}
public long getDurata() {
return durata;
}
public String getThumbnailFilePath() {
return miniaturaFilePath;
}
public String toString() {
return "Video [title=" + titolo + ", PathFileVideo=" + videoFilePath
+ ", mimeType=" + mimeType + ", durata=" + durata
+ ", thumbnailFilePath=" + miniaturaFilePath + "]";
}
}
这是MovieListAdapter
package com.example.movieplayer;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;
@SuppressLint({ "InflateParams", "DefaultLocale" })
class MovieListAdapter extends BaseAdapter {
private Context context;
ArrayList<Movie> videoList;
public MovieListAdapter(Context context, ArrayList<Movie> movieList) {
this.context = context;
this.videoList = movieList; }
@Override
public int getCount() {
return videoList.size();
}
@Override
public Object getItem(int position) {
return videoList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.movie_item, null);
}
Movie movie = (Movie) getItem(position);
// thumbnail
ImageView thumbnail =
(ImageView) convertView.findViewById(R.id.thumbnail);
if (movie.getThumbnailFilePath() != null) {
thumbnail.setImageURI(Uri.parse(movie.getThumbnailFilePath()));
} else {
thumbnail.setImageResource(R.drawable.ic_launcher);
}
//titolo
TextView titolo = (TextView) convertView.findViewById(R.id.titolo);
titolo.setText(movie.getTitolo());
//durata
TextView durata = (TextView) convertView.findViewById(R.id.durata);
durata.setText(getDurataAsString(movie.getDurata()));
return convertView;
}
private static String getDurataAsString(long durata) {
long secondi = durata / 1000;
long millisecondi = durata % 1000;
long minuti = secondi / 60;
secondi %= 60;
long ore = minuti / 60;
minuti %= 60;
String durataString = String.format("%1$02d:%2$02d:%3$02d.%4$03d",
ore, minuti, secondi, millisecondi);
return durataString;
}
}
这是LogCat在应用程序崩溃时给我的错误。
09-08 00:04:42.681: E/AndroidRuntime(9468): FATAL EXCEPTION: main
09-08 00:04:42.681: E/AndroidRuntime(9468): Process: com.example.movieplayer, PID: 9468
09-08 00:04:42.681: E/AndroidRuntime(9468): android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=android.intent.action.VIEW
dat=/storage/sdcard0/dcim/Camera/VID_20150804_193111.mp4 typ=video/mp4 }
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.Instrumentation.checkStartActivityResult
(Instrumentation.java:1776)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.Instrumentation.execStartActivity
(Instrumentation.java:1496)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.Activity.startActivityForResult(Activity.java:3801)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.Activity.startActivityForResult(Activity.java:3743)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.Activity.startActivity(Activity.java:4072)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.Activity.startActivity(Activity.java:4040)
09-08 00:04:42.681: E/AndroidRuntime(9468): at com.example.movieplayer.MoviePlayerActivity.onItemClick
(MoviePlayerActivity.java:85)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.widget.AdapterView.performItemClick(AdapterView.java:305)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.widget.AbsListView.performItemClick(AbsListView.java:1185)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3222)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.widget.AbsListView$3.run(AbsListView.java:4138)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.os.Handler.handleCallback(Handler.java:815)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.os.Handler.dispatchMessage(Handler.java:104)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.os.Looper.loop(Looper.java:194)
09-08 00:04:42.681: E/AndroidRuntime(9468): at android.app.ActivityThread.main(ActivityThread.java:5578)
09-08 00:04:42.681: E/AndroidRuntime(9468): at java.lang.reflect.Method.invoke(Native Method)
09-08 00:04:42.681: E/AndroidRuntime(9468): at java.lang.reflect.Method.invoke(Method.java:372)
09-08 00:04:42.681: E/AndroidRuntime(9468): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:955)
09-08 00:04:42.681: E/AndroidRuntime(9468): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
任何人都知道这是什么问题?
答案 0 :(得分:4)
替换:
Uri.parse(movie.getMovieFilePath()
使用:
Uri.fromFile(new File(movie.getMovieFilePath()))
看看是否有帮助。在您的错误消息中,Uri
中的Intent
没有任何方案,此更改应解决该问题。