我正在创建一个应用程序,它从库中上传所选图像并将其上传到Web服务.Web服务需要所选图像的文件名以及文件内容的base64编码.我已设法通过硬编码文件路径实现此目的.但是,我正在努力获得图像的真实文件路径.我已经在网上阅读并拥有此代码,但它对我不起作用:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
try {
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.d("Picture Path", picturePath);
}
catch(Exception e) {
Log.e("Path Error", e.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
编辑
忘了提我正在使用Kitkat.看起来我的问题与KitKat有关.我发现这个(见下文)帮助我让我的应用程序正常工作:
伙计们我试图将以_DONE结尾的所有文件移动到另一个文件夹中.
我试过了
//take all files of main folder to folder model_RCCMrecTransfered
string rootFolderPath = @"F:/model_RCCMREC/";
string destinationPath = @"F:/model_RCCMrecTransfered/";
string filesToDelete = @"*_DONE.wav"; // Only delete WAV files ending by "_DONE" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach (string file in fileList)
{
string fileToMove = rootFolderPath + file;
string moveTo = destinationPath + file;
//moving file
File.Move(fileToMove, moveTo);
Run Code Online (Sandbox Code Playgroud)
但在执行这些代码时,我得到一个错误说.
The given path's format is not supported.
Run Code Online (Sandbox Code Playgroud)
我哪里做错了 ?
我尝试使用AFNetworking多部分表单数据进行文件上传操作.我收到了以下错误.我可以找出什么是错误.
[NSURL URLWithString:filePath]
也用过[[NSURL URLWithString:filePath] filePathURL],但不帮助我.当我记录文件路径字符串时:它显示正确的路径:/var/mobile/Applications/3CBF5127-B2FF-49C3-AC98-16BD0886EEE7/Documents/20140326105108_slno.ma4
错误:
@"NSLocalizedFailureReason" : @"Expected URL to be a file URL"
Questions:如何将此路径字符串转换为file url?
目标是在这些Xml文件中给出一些数据来运行一些测试.
如何在单元测试方法中轻松地将给定的Xml文件加载到XmlDoc中?
目前的状态是:
XmlDocument doc = new XmlDocument();
string xmlFile = "4.xml";
string dir = System.IO.Directory.GetCurrentDirectory() + @"\Msgs\"
//dir is then the value of the current exe's path, which is
//d:\sourcecode\myproject\TestResults\myComputer 2009-10-08 16_07_45\Out
//we actually need:
//d:\sourcecode\myproject\Msgs\
doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!
Run Code Online (Sandbox Code Playgroud)
只是将这条路径放在一个简单的问题上app.config?考虑到开发人员机器上存在不同路径的可能性,我希望避免这种情况.
问题:如何编写算法以将单个测试方法中的给定Xml文件加载到XmlDocument中?
这是问题所在.经过一些连接,我可能碰巧有这样的字符串
"C:/shared_resources/samples\\import_packages\\catalog.zip"
Run Code Online (Sandbox Code Playgroud)
甚至这个
"C:/shared_resources/samples/subfolder/..\\import_packages\\catalog.zip"
Run Code Online (Sandbox Code Playgroud)
我想要一些代码将这种字符串转换为具有统一分隔符的路径.
想到的第一个解决方案是使用new File(srcPath).getCanonicalPath(),但这里是棘手的部分.此方法依赖于调用测试的系统.但是我需要将字符串传递给远程计算机(带有浏览器的Selenium Grid节点),这里和那里的系统分别是Linux和Windows.因此,尝试做new File("C:/shared_resources/samples\\import_packages\\catalog.zip").getCanonicalPath()类似的结果"/home/username/ourproject/C:/shared_resources/samples/import_packages/catalog.zip".使用钝性正则表达式替换似乎也不是一个非常好的解决方案.
有没有办法修剪路径并使分隔符统一(并且可能解决..它)而不试图隐含地绝对化它?
我正在尝试在C#中编写一个静态成员函数,或者在.NET Framework中找到一个可以重写文件系统指定文件路径的函数.
例:
string filepath = @"C:\temp.txt";
filepath = FileUtility.RecaseFilepath(filepath);
// filepath = C:\Temp.TXT
// Where the real fully qualified filepath in the NTFS volume is C:\Temp.TXT
Run Code Online (Sandbox Code Playgroud)
我已经尝试了下面的代码和它的许多变体,它仍然无法正常工作.我知道Windows一般不区分大小写但我需要将这些文件路径传递给ClearCase,后者考虑文件路径大小写,因为它是Unix和Windows应用程序.
public static string GetProperFilePathCapitalization(string filepath)
{
string result = "";
try
{
result = Path.GetFullPath(filepath);
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(result));
FileInfo[] fi = dir.GetFiles(Path.GetFileName(result));
if (fi.Length > 0)
{
result = fi[0].FullName;
}
}
catch (Exception)
{
result = filepath;
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我无法控制的子系统坚持以uri的形式提供文件系统路径.是否有一个python模块/函数可以将此路径转换为文件系统所期望的适当形式,并且与平台无关?
我写了一个脚本来读取python中的文本文件.
这是代码.
parser = argparse.ArgumentParser(description='script')
parser.add_argument('-in', required=True, help='input file',
type=argparse.FileType('r'))
parser.add_argument('-out', required=True, help='outputfile',
type=argparse.FileType('w'))
args = parser.parse_args()
try:
reader = csv.reader(args.in)
for row in reader:
print "good"
except csv.Error as e:
sys.exit('file %s, line %d: %s' % (args.in, reader.line_num, e))
for ln in args.in:
a, b = ln.rstrip().split(':')
Run Code Online (Sandbox Code Playgroud)
我想检查文件是否存在而不是空文件但是这段代码给了我一个错误.
我还想检查程序是否可以写入输出文件.
命令:
python script.py -in file1.txt -out file2.txt
Run Code Online (Sandbox Code Playgroud)
错误:
good
Traceback (most recent call last):
File "scritp.py", line 80, in <module>
first_cluster = clusters[0]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud) 涉及到我刚才的问题在这里.
我有资源库中图像的路径,例如:
assets-library://asset/asset.JPG?id=1000000001&ext=JPG
Run Code Online (Sandbox Code Playgroud)
现在,我如何将此路径中的图像加载到UIImage对象?
这是代码:
NSString *path = [occasion imagePath];
//temp
NSLog(@"the 2nd occasion imagePath is: %@", path);
//end
if (path != nil)
{
//UIImage *image = [UIImage imageNamed:path];
UIImage *image = [UIImage imageWithContentsOfFile:path];
image = [image imageByScalingAndCroppingForSize:CGSizeMake(36.0, 42.0)];
[[cell imageView] setImage:image];
}
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的目录:
C:\Users\me\Projects\
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我将该路径附加到给定的项目名称:
C:\Users\me\Projects\myProject
Run Code Online (Sandbox Code Playgroud)
之后,我希望能够将其传递给方法.在这个方法里面我也想使用项目名称.解析路径字符串以获取最后一个文件夹名称的最佳方法是什么?
我知道解决方法是将路径和项目名称传递给函数,但我希望我可以将它限制为一个参数.