【问题标题】:How to get file from destination path如何从目标路径获取文件
【发布时间】:2018-03-14 10:47:03
【问题描述】:

我不知道如何打开NSFileManager。 如何在 iPhone 中打开NSFileManager 并从NSFileManager 上传文档请建议任何简单的方法。

如何打开并上传文件并获取保存文件的路径。

我可以在哪里找到文件。(任何位置)。

::EDIT::

我从那一年开始编码。所以,我不知道NSFileManager的基本知识。

【问题讨论】:

  • @Mukesh 可以像对话框一样工作吗?我可以从中选择文档吗?
  • 这可能是一个 A-B 问题,你能描述一下你的背景细节吗?

标签: objective-c iphone nsfilemanager


【解决方案1】:

显示内容:

 NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); 

获取所有文件

//get an instance of the File Manager
NSFileManager *fileManager = [NSFileManager defaultManager];

   //we'll list file in the temporary directory

NSString * strPath = NSTemporaryDirectory();

//we'll need NSURL for the File Manager
NSURL *tempDirURL = [NSURL fileURLWithPath:strPath];


  //An array of NSURL object representing the path to the file
    //using the flag NSDirectoryEnumerationSkipsHiddenFiles to skip hidden files

NSArray *directoryList = [fileManager contentsOfDirectoryAtURL:tempDirURL 
                      includingPropertiesForKeys:nil 
              options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];

参考:http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files

http://nshipster.com/nsfilemanager/

【讨论】:

  • 我没有给你什么
【解决方案2】:

iPhone 的文档目录用于保存数据。我们可以保存一些个人数据,如文件、图片、视频等。文档目录的数据会一直保留在 iPhone 的内存中,直到应用程序被强制终止。

让我们看一个示例,说明如何在 iPhone 中存储一些图像并稍后检索这些图像。

// For saving the images in the Document's Directory

-(void)saveImagesInIPhone:(NSData*)imageData withName:(NSString*)imageName

{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

 //Get the docs directory

NSString *documentsDirectoryPath = [paths objectAtIndex:0]; 

NSString *folderPath = [documentsDirectoryPath  stringByAppendingPathComponent:@"IconImages"];  // subDirectory

 if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath])

    [[NSFileManager defaultManager] createDirectoryAtPath:folderPath  withIntermediateDirectories:NOattributes:nil error:nil];



//Add the FileName to FilePath

NSString *filePath = [folderPath stringByAppendingPathComponent:[iconName   stringByAppendingFormat:@"%@",@".png"]];

//Write the file to documents directory

[imageData writeToFile:filePath atomically:YES]; 

}

结果:- /var/mobile/Applications/20F869FD-5C61-4900-8CFE-830907731EC9/Documents/Designer0.png


  ///To retrieve the images from the document Directory

 -(UIImage*)retrieveImageFromPhone:(NSString*)fileNamewhichtoretrieve

{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  

 //Get the docs directory

NSString *documentsPath = [paths objectAtIndex:0]; 


NSString *folderPath = [documentsPath   stringByAppendingPathComponent:@"IconImages"];  // subDirectory


NSString *filePath = [folderPath stringByAppendingPathComponent:

                      [fileNamewhichtoretrieve stringByAppendingFormat:

                        @"%@",@".png"]];


if([[NSFileManager defaultManager] fileExistsAtPath:filePath])

    return [[UIImage alloc] initWithContentsOfFile:filePath];

else 

    return nil;

}

结果:- /var/mobile/Applications/20F869FD-5C61-4900-8CFE-830907731EC9/Documents/Designer0.png

【讨论】:

  • 我有所有路径和相关查询,但不像安卓文件管理器那样打开,我们从它存储的地方拍摄照片、文档等。
  • 不,您不能这样做,这与 android 中的文件管理器应用程序不同。您只能以编程方式执行操作。
  • 是的,兄弟,为什么不呢
【解决方案3】:

你可以像这样获取目录路径..

-(NSString *)getDBPath
{
    //Searching a standard documents using NSSearchPathForDirectoriesInDomains
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [paths objectAtIndex:0];
    NSLog(@"I am in --> getDBPath==%@",paths);

    return [documentDir stringByAppendingPathComponent:@"abcd_DB.sqlite"];
}

**如果您使用的是 iOS 设备- 您可以使用https://macroplant.com/iexplorer Application 直接获取您的数据库。

**如果您使用的是模拟器。

  1. 获取Path目录

  2. 在终端中:打开目录路径(过去的目录路径)。

  3. 复制您的数据库并使用 Firefox 或 sqlite 浏览器进行浏览。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    相关资源
    最近更新 更多