Android:保存的视频文件无法播放

时间:2015-11-25 07:15:41

标签: android-videoview android-gallery

我开发了一款可以播放图库视频的应用,然后将播放的视频保存回图库中的文件夹中。该应用程序工作正常,因为我可以从库中选择和播放视频,然后保存它。点击“保存视频”按钮后,“视频已保存!”消息弹出窗口和保存的视频都在画廊中找到。现在的问题是我无法播放视频,它说“对不起,这个视频无法播放”。我无法确定哪里出错了。我的编码如下:

AndroidVideoPlayer.java:

public class AndroidVideoPlayer extends Activity {
/**
 * Called when the activity is first created.
 */
Button button, button2;
VideoView videoView;
private static final int PICK_FROM_GALLERY = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_android_video_player);

    button = (Button) findViewById(R.id.button);
    button2 = (Button) findViewById(R.id.savevideo);
    videoView = (VideoView) findViewById(R.id.videoview);

    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();

            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
        }
    });


    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            savequesimage();
           // Toast.makeText(getActivity(), "Sample Answer Saved", Toast.LENGTH_LONG).show();




        }


    });
 }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) return;

    if (requestCode == PICK_FROM_GALLERY) {
        Uri mVideoURI = data.getData();
        videoView.setVideoURI(mVideoURI);
        videoView.start();
    }

}


protected boolean savequesimage() {


    boolean success = false;
    View content = videoView;
    content.invalidate();
    // make the directory
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SavedVideo/";
    File dir = new File(path);
    if(!dir.exists())
        dir.mkdirs();;
    // create unique identifier
    Random generator = new Random();
    int n = 100;
    n = generator.nextInt(n);
    // create file name
    String videoName = "Video_" + n + ".mp4";
    File fileVideo = new File(dir.getAbsolutePath(), videoName);

    try {
        fileVideo.createNewFile();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (success) {
        Toast.makeText(getApplicationContext(), "Video saved!",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during video saving", Toast.LENGTH_LONG).show();
    }

    return true;

}

}

activity_android_video_player.xml:

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

<Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="- PLAY Video -"
    />
<Button
    android:id="@+id/savevideo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="- Save Video -"
    />


<VideoView
    android:id="@+id/videoview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />


</LinearLayout>

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mainapp.videoview" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".AndroidVideoPlayer"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

有人可以帮我解决这个问题吗?任何帮助/建议都会非常有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

首先,添加权限READ_EXTERNAL_STORAGE&amp;对AndroidManifest.xml的WRITE_EXTERNAL_STORAGE如下:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果Android级别&gt; = 23,您需要在代码中询问权限:

// With Android Level >= 23 you need ask permission.
if (android.os.Build.VERSION.SDK_INT >= 23) {

    // Check if we have write & read permission 
    int writePermission = this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int readPermission = this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);

    if (writePermission != PackageManager.PERMISSION_GRANTED ||
            readPermission != PackageManager.PERMISSION_GRANTED) {
        // If don't have permission so prompt the user. 
        this.requestPermissions(
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE},
                REQUEST_PERMISSION
        );
    }
}

在此处查看更多信息。