首页 > 其他分享 >intent的方法使用

intent的方法使用

时间:2023-06-22 22:05:25浏览次数:16  
标签:data Uri intent RESULT 使用 Intent theIntent null 方法

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

标签:data,Uri,intent,RESULT,使用,Intent,theIntent,null,方法
From: https://blog.51cto.com/u_16166892/6535797

相关文章

  • ConcurrentHashMap的使用场景
    一、并发容器ConcurrentHashMapHashMap是我们用得非常频繁的一个集合,但是它是线程不安全的。并且在多线程环境下,put操作是有可能产生死循环,不过在JDK1.8的版本中更换了数据插入的顺序,已经解决了这个问题。为了解决该问题,提供了Hashtable和Collections.synchronizedMap(hashMap)......
  • ConcurrentHashMap使用案例(单词数量统计)
    前言目标:实现单词数量统计过程:首先使用26个英文字母,每个字母200个,将26200个字母打乱顺序存入26个txt文件中。使用26个线程,每个线程统计一个txt文件的200个字母。26个线程同时操作这一个Map集合。最终想要得到的结果为:a:200(a被统计了200次),b:200(b被统计了200次)……z:200(z被统计200......
  • PostgreSQL 时间函数 extract函数和epoch 新纪元时间的使用
    Extract属于SQL的DML(即数据库管理语言)函数,同样,InterBase也支持Extract,它主要用于从一个日期或时间型的字段内抽取年、月、日、时、分、秒数据,因此,它支持其关健字YEAR、MONTH、DAY、HOUR、MINUTE、SECOND、WEEKDAY、YEARDAY。计算时间差天数selectextract(dayFROM(age(......
  • JAVA:Collections类的shuffle()方法
    Java.util.Collections类下有一个静态的shuffle()方法,如下:1)staticvoidshuffle(List<?>list)使用默认随机源对列表进行置换,所有置换发生的可能性都是大致相等的。2)staticvoidshuffle(List<?>list,Randomrand)使用指定的随机源对指定列表进行置换,所有置换发生的可能性......
  • cmake简介及基本使用说明
    CMAKE是一个开源、跨平台的编译、测试和打包工具,它使用比较简单的语言描述编译、安装的过程,输出Makefile或者project文件,再去执行构建。在linux环境下编写C++程序是没有类似windows下编译器的一键编译和运行的按钮,只能由开发人员进行手动编译、运行;当多人协同开发一个较大的项目......
  • 如何使用开源拉新插件拉新
    当使用开源拉新插件时,通常需要在小程序代码中添加插件代码和相关配置。以下是一个使用Weirank开源拉新插件的示例代码分析:分析前我们来吟诗一首方便联系:【名】扬天下志在成,【字】里行间传真情,【拼】搏奋斗路漫漫,【音】乐轻扬心更静,【就】算跌倒也不怕,【是】非得失忠于心,【微】笑面......
  • 如何使用开源拉新插件拉新
    当使用开源拉新插件时,通常需要在小程序代码中添加插件代码和相关配置。以下是一个使用Weirank开源拉新插件的示例代码分析:分析前我们来吟诗一首方便联系:【名】扬天下志在成,【字】里行间传真情,【拼】搏奋斗路漫漫,【音】乐轻扬心更静,【就】算跌倒也不怕,【是】非得失忠于心,【微】笑面......
  • Go的pprof使用
    Go中监控代码性能的有两个包:net/http/pprofruntime/pprof这两个包都是可以监控代码性能的,只不过net/http/pprof是通过http端口方式暴露出来的,内部封装的仍然是runtime/pprof。 runtime/pprof的用法示例这里的例子我们用递归实现的斐波纳契数列来测试性能,斐波纳契数列的代码......
  • Android 使用 TableLayout 布局拉伸宽度
    布局文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent" android:lay......
  • PostgreSQL被除数为0的处理方法
    PostgreSQL被除数为0的处理方法方法一:使用case语法selectcasewhenid=0then0elseid/2endfromc2;方法二:自定义操作符CREATEORREPLACEFUNCTIONDIV_ZERO(NUMERIC,NUMERIC) RETURNSNUMERICAS$BODY$ SELECTCASEWHEN$2=0THEN0ELSE$1/$2END; $BODY$ L......