Android:如何将uA从onActivity结果传递到另一种方法?

时间:2015-12-01 04:15:09

标签: android android-intent android-activity uri

如何将uA从onActivity结果传递到同一个jave文件中的另一个方法。

@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();
    }

}

方法savevideo:

public void savevideo() {


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;

}

}

我想将mVideoURI传递给savevideo方法,然后将视频uri保存到图库中。有人可以帮我弄这个吗。任何指导/建议都会非常有帮助。谢谢。

编辑:完整编码:

public class AndroidVideoPlayer extends Activity {

Button button;
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);

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

}

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

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

}
public void savevideo(Uri mVideoURI) {


    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);

    boolean success=false;
    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();
    }



}

}

3 个答案:

答案 0 :(得分:1)

根据我对您问题的理解,您必须在从其他活动获得结果后调用方法。所以你可能会被称为startActivitForResult(activityB)来获取视频Uri。因此,您将从activityB获得回调,因此您可以直接将视频传递给方法saveVideo(),因为它位于同一个活动中。

示例

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

    if (requestCode == PICK_FROM_GALLERY) {
        Uri mVideoURI = data.getData();
        saveVideo(videoUri);
    }
}

public void saveVideo(Uri videoUri){
  // do operations with uri
}

或者如果您不能接受saveVideo()方法中的任何参数,则可以将uri作为成员变量并使用inside()方法

答案 1 :(得分:1)

使用此代码 -

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

    if (requestCode == PICK_FROM_GALLERY) {
        Uri mVideoURI = data.getData();
        saveVideo(mVideoURI);                // methode to save uri gets called here
        videoView.setVideoURI(mVideoURI);
        videoView.start();
    }

}
method savevideo:

public void savevideo(Uri mVideoURI) {


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;

}

}

答案 2 :(得分:0)

在您的代码中,您只在目标路径上创建新文件,但不将源文件的数据写入目标文件。

所以你得到的是0KB文件。使用以下代码编写文件。

void savefile(URI sourceuri)
{
String sourceFilename= sourceuri.getPath();
String destinationFilename = android.os.Environment.getExternalStorageDirectory().getPath()+File.separatorChar+"abc.mp3";

BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {
  bis = new BufferedInputStream(new FileInputStream(sourceFilename));
  bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
  byte[] buf = new byte[1024];
  bis.read(buf);
  do {
    bos.write(buf);
  } while(bis.read(buf) != -1);
} catch (IOException e) {

} finally {
  try {
    if (bis != null) bis.close();
    if (bos != null) bos.close();
  } catch (IOException e) {

  }
}
}