intentの方法使用

24698 ワード

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) == Uri.parse("file://"+aFile.getPath()) 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";      Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);     theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,aFileList);     theIntent.setType("audio/*");     theIntent.putExtra(EXTRA_DIRECTORY,theDefaultFolderPath); //optional     try {         startActivity(theIntent);     } catch (Exception e) {         e.printStackTrace();     } } 4. 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";     public static final Uri MIMETYPE_URI = Uri.parse("content://" + MIME_AUTHORITY ); 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;     } }