Android 将打开应用程序的文件复制到应用程序中的某个临时文件夹中
我正在使用自己的扩展名“.swp”将文件与我的应用程序关联。当用户试图打开具有该扩展名的文件时,我可以选择我的应用程序。例如,如果sd卡中的任何位置都有文件“file1.swp”,并且如果用户单击该文件,我的应用程序也将在打开该应用程序的选项列表中可用。但我想在它选择打开我的应用程序时,将“file1.swp”保存在某个临时文件夹中Android 将打开应用程序的文件复制到应用程序中的某个临时文件夹中,android,android-intent,Android,Android Intent,我正在使用自己的扩展名“.swp”将文件与我的应用程序关联。当用户试图打开具有该扩展名的文件时,我可以选择我的应用程序。例如,如果sd卡中的任何位置都有文件“file1.swp”,并且如果用户单击该文件,我的应用程序也将在打开该应用程序的选项列表中可用。但我想在它选择打开我的应用程序时,将“file1.swp”保存在某个临时文件夹中 任何建议请………我将我的全部代码发布在下面,以便其他有相同问题的人可以参考: String strFilename = "Not set"; Int
任何建议请………我将我的全部代码发布在下面,以便其他有相同问题的人可以参考:
String strFilename = "Not set";
Intent intent = getIntent();
if(intent != null)
{
Uri uri = intent.getData();
if(uri != null)
{
strFilename = intent.getData().getPath();
String NameOfFile = intent.getDataString();
Toast.makeText(SplashActivity.this, "strFilename" + strFilename + "" + "Name of file is::" + NameOfFile, Toast.LENGTH_LONG).show();
System.out.println("strFileName::"+ strFilename );
System.out.println("NameOfFile::"+ NameOfFile );
try {
copyFile(strFilename,"/mnt/sdcard/Profile.swp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
}
}
//copy file into other location
private void copyFile(String assetFilename, String outFileName ) throws IOException{
System.out.println("outFileName ::" + outFileName);
//Open your local db as the input stream
InputStream myInput = new FileInputStream(assetFilename);
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[2048];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
您的意思是当用户单击file1.swp文件并选择用您的应用程序打开此文件时,需要将file1.swp存储到temp中吗folder@Pratik:yes file1.swp应该存储在temp文件夹中,以便我以后可以使用该文件。但是您的应用程序是否打开该文件?@pratik:还没有,当在该文件上单击时,我的应用程序将被打开,用户必须单击导入才能在listview中填充该文件的数据。我希望存储在临时文件夹中的原因是:当用户单击“导入”时,我将显示警报,提示您确实要导入此文件吗?如果是,则只有我将在数据库中输入这些数据,并在listview中显示内容。我希望这个解释能有所帮助…………你使用过意图过滤器吗?你找到了答案:)