Android:上传视频时找不到文件异常

时间:2012-09-26 07:47:58

标签: android filenotfoundexception android-file

我正在尝试使用post方法将视频上传到服务器,但我正在

  

FileNotFoundException

这是我的代码。在路径变量中,我得到“content:/ media / external / video / media / 2”

public class VideoPlayUploadActivity extends Activity {
private VideoView video_play;
private Button accept_and_go;
private Button reject_and_do;
Uri vid;
String user;
String question;

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

    video_play = (VideoView) findViewById(R.id.videoView1);
    accept_and_go = (Button) findViewById(R.id.button_accept_and_go_over_video);
    reject_and_do = (Button) findViewById(R.id.button_reject_and_do_over_video);

    Intent extras = getIntent();
    vid = Uri.parse(extras.getStringExtra("uri"));

    if (vid == null) {
        Toast.makeText(VideoPlayUploadActivity.this, "No Video",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(VideoPlayUploadActivity.this,
                "Playback: " + vid.getPath(), Toast.LENGTH_LONG).show();
        MediaController mc = new MediaController(this);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        video_play.setMediaController(mc);
        video_play.setVideoURI(vid);
        video_play.start();

    }
    accept_and_go.setOnClickListener(accept_go_listener);
}

private View.OnClickListener accept_go_listener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        // TODO Auto-generated method stub
        String path = vid.getPath();
        File file = new File(path);
        try {
            HttpClient client = new DefaultHttpClient();
            String postURL = "xxxxxxxxxxxxxxxxxxxxxxxxupdateAnswerFile";
            HttpPost post = new HttpPost(postURL);
            FileBody bin = new FileBody(file);
            StringBody user = new StringBody("1");
            StringBody question = new StringBody("25");
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("user", user);
            reqEntity.addPart("question", question);
            reqEntity.addPart("answer", bin);
            post.setEntity(reqEntity);
            HttpResponse response = client.execute(post);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                Log.i("RESPONSE", EntityUtils.toString(resEntity));
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "exception", 0).show();
            e.printStackTrace();
        }
    }
};
private View.OnClickListener reject_do_listenter = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
};
}

请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:1)

请按照以下链接

Get filename and path from URI from mediastore

它显示错误,因为您没有将内容路径转换为实际路径。

它对我有用。

希望它有所帮助。