首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

intent的步骤使用

2012-09-02 
intent的方法使用1.Pick Filevoid pickFile(File aFile) {??? Intent theIntent new Intent(Intent.ACTI

intent的方法使用

1.Pick File

void pickFile(File aFile) {
??? Intent theIntent = new Intent(Intent.ACTION_PICK);
??? theIntent.setData(Uri.fromFile(aFile));? //default file / jump directly to this file/folder
??? theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
??? theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
??? try {
??? ??? startActivityForResult(theIntent,PICK_FILE_RESULT_CODE);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
??? switch (requestCode) {
??? ??? case PICK_FILE_RESULT_CODE: {
??? ??? ??? if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
??? ??? ??? ??? String theFilePath = data.getData().getPath();
??? ??? ??? ??? ...
??? ??? ??? }
??? ??? ??? break;
??? ??? }
??? }
}

Uri.fromFile(aFile) ==2.

void pickFiles(File aFolder) {
??? Intent theIntent = new Intent(Intent.ACTION_PICK);
??? theIntent.setData(Uri.fromFile(aFolder));? //jump directly to this folder
??? theIntent.putExtra("com.blackmoonit.extra.ALLOW_MULTIPLE",true); //required
??? theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
??? theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
??? try {
??? ??? startActivityForResult(theIntent,PICK_FILES_RESULT_CODE);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
??? switch (requestCode) {
??? ??? case PICK_FILES_RESULT_CODE: {
??? ??? ??? if (resultCode==RESULT_OK && data!=null && data.getExtras()!=null) {
??? ??? ??? ??? ArrayList<Uri> theFileUriList = data.getExtras().get(Intent.EXTRA_STREAM);
??? ??? ??? ??? ...
??? ??? ??? }
??? ??? ??? break;
??? ??? }
??? }
}

void pickFolder(File aFolder) {
??? Intent theIntent = new Intent(Intent.ACTION_PICK);
??? theIntent.setData(Uri.parse("folder://"+aFolder.getPath()));? //default folder / jump directly to this folder
??? theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
??? theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
??? try {
??? ??? startActivityForResult(theIntent,PICK_FOLDER_RESULT_CODE);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
??? switch (requestCode) {
??? ??? case PICK_FOLDER_RESULT_CODE: {
??? ??? ??? if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
??? ??? ??? ??? String theFolderPath = data.getData().getPath();
??? ??? ??? ??? ...
??? ??? ??? }
??? ??? ??? break;
??? ??? }
??? }
}

2.

void saveToFile(File aFile) {
??? Uri theUri = Uri.fromFile(aFile).buildUpon().scheme("file.new").build();
??? Intent theIntent = new Intent(Intent.ACTION_PICK);
??? theIntent.setData(theUri);
??? theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
??? theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
??? try {
??? ??? startActivityForResult(theIntent,SAVE_FILE_RESULT_CODE);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
??? switch (requestCode) {
??? ??? case SAVE_FILE_RESULT_CODE: {
??? ??? ??? if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
??? ??? ??? ??? String theFilePath = data.getData().getPath();
??? ??? ??? ??? ...
??? ??? ??? }
??? ??? ??? break;
??? ??? }
??? }
}

void saveToFolder(File aFolder) {
??? Uri theUri = Uri.fromFile(aFolder).buildUpon().scheme("folder.new").build();
??? Intent theIntent = new Intent(Intent.ACTION_PICK);
??? theIntent.setData(theUri);
??? theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
??? theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
??? try {
??? ??? startActivityForResult(theIntent,SAVE_FOLDER_RESULT_CODE);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
??? switch (requestCode) {
??? ??? case SAVE_FOLDER_RESULT_CODE: {
??? ??? ??? if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
??? ??? ??? ??? String theFolderPath = data.getData().getPath();
??? ??? ??? ??? ...
??? ??? ??? }
??? ??? ??? break;
??? ??? }
??? }
}

3.

private static final String EXTRA_DIRECTORY = "com.blackmoonit.intent.extra.DIRECTORY";

void createPlaylist(ArrayList<Uri> aFileList) {
??? String theDefaultFolderPath = "/sdcard/playlists";

Create Zip file

private static final String EXTRA_DIRECTORY = "com.blackmoonit.intent.extra.DIRECTORY";

void createZipFile(ArrayList<Uri> aFileList) {
??? Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
??? theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,aFileList);
??? theIntent.setType("multipart/mixed");? //this should be a good faith attempt at determining the MIME type
??? String theFolderPath = "/sdcard/some_folder";? //all files in the Zip will be stored relative to this path
??? theIntent.putExtra(EXTRA_DIRECTORY,theFolderPath);
??? try {
??? ??? startActivity(theIntent);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

void createZipFile(File aFile) {
??? Intent theIntent = new Intent(Intent.ACTION_SEND);
??? theIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(aFile));
??? try {
??? ??? startActivity(theIntent);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

void unpackZipFile(File aFile) {
??? Intent theIntent = new Intent(Intent.ACTION_VIEW);
??? theIntent.setDataAndType(Uri.fromFile(aFile),"application/zip");
??? try {
??? ??? startActivity(theIntent);
??? } catch (Exception e) {
??? ??? e.printStackTrace();
??? }
}

public static final String MIME_AUTHORITY = "com.blackmoonit.FileBrowser.mimeType";???

private String getMIMEtypeFromProvider(String aFilename) {
??? Uri theContentTypeRequest = Uri.withAppendedPath(MIMETYPE_URI,aFilename);
??? Cursor theTypeResult = managedQuery(theContentTypeRequest, null, null, null, null);
??? theTypeResult.moveToFirst();
??? if (!theTypeResult.isNull(0)) {
??? ??? return theTypeResult.getString(0);
??? } else {
??? ??? return null;
??? }
}