当前位置: 编程技术>移动开发
本页文章导读:
▪依据音乐文件和LRC文件的绝对路径下载到sdcard指定目录中 根据音乐文件和LRC文件的绝对路径下载到sdcard指定目录中
/**
* 下载音乐文件到指定目录
* @param musicPath
* @param musicName
* @param singerName
*/
public void downloadMusic(final String musicPath, String musicN.........
▪ 公共视图的兑现 公共视图的实现
如上图所示,页面分为两个部分:上半部分是一个UIDatePicker,下半部分是一个UITableView。这两部分可以同时放在同一个XIB里,但是,如果UITableView是一个公共视图的话,也.........
▪ Objective-C Unicode 转换成汉语【转】 Objective-C Unicode 转换成中文【转】
+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString *tempStr2 .........
[1]依据音乐文件和LRC文件的绝对路径下载到sdcard指定目录中
来源: 互联网 发布时间: 2014-02-18
根据音乐文件和LRC文件的绝对路径下载到sdcard指定目录中
下载歌词时要注意inputStream的转码,由于服务器中的xml文件是用gb2312编码,所以应按照原来的格式读进来
其中创建文件和文件目录代码如下:
[img]
[/img]
参考资料:
Android中文乱码彻底解决
http://www.iteye.com/topic/509046
Android 中下载文件到sdcard和进度条小结
http://www.linuxidc.com/Linux/2011-08/40941.htm
Android中的自动下载歌词
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=61595
/**
* 下载音乐文件到指定目录
* @param musicPath
* @param musicName
* @param singerName
*/
public void downloadMusic(final String musicPath, String musicName,
String singerName ) {
String dir = "ccpod/downloadMusic";
final String filePath = "/sdcard/ccpod/downloadMusic/" +
musicName +"-"+ singerName +".mp3";
FileService fileService = new FileService();
fileService.createFileDir(dir);
fileService.createFile(filePath);
Runnable r = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
int count;
try {
URL url = new URL(/blog_article/musicPath/index.html);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(filePath);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
System.out.println(count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("error",e.getMessage().toString());
System.out.println(e.getMessage().toString());
}
}
};
new Thread(r).start();
}
/**
* 下载歌词文件到指定目录
* @param lrcPath
* @param musicName
* @param singerName
*/
public void downloadLyric(final String lrcPath, String musicName,
String singerName) {
String dir = "ccpod/lyric";
final String filePath = "/sdcard/ccpod/lyric/" + musicName + "-"
+ singerName + ".lrc";
FileService fileService = new FileService();
fileService.createFileDir(dir);
fileService.createFile(filePath);
Runnable r = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(/blog_article/lrcPath/index.html);
InputStream input = new BufferedInputStream(
url.openStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(input,"GB2312"));
Writer writer = new OutputStreamWriter(new FileOutputStream(filePath));
String data = "";
while ((data = br.readLine()) != null) {
sb.append(data+ "\r\n");
}
writer.write(sb.toString());
writer.close();
input.close();
} catch (Exception e) {
Log.e("error", e.getMessage().toString());
}
}
};
new Thread(r).start();
}下载歌词时要注意inputStream的转码,由于服务器中的xml文件是用gb2312编码,所以应按照原来的格式读进来
其中创建文件和文件目录代码如下:
FileService.java
public boolean createFileDir(String dirName) {
dirName = Environment.getExternalStorageDirectory() + "/" + dirName;
File dir = new File(dirName);
if (dir.exists()) {
return false;
}
if (!dirName.endsWith("/")) {
dirName += File.separator;
}
if (dir.mkdirs()) {
return true;
} else
return false;
}
public boolean createFile(String fileName){
File file = new File(fileName);
if(file.exists()){
System.out.println("创建单个文件:"+ fileName + "失败,目标文件已存在");
return false;
}
if(!file.getParentFile().exists()){
System.out.println("目标文件所在目录不存在,准备创建它");
return false;
}
try {
if(file.createNewFile()){
return true;
}else{
return false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}[img]
[/img]
参考资料:
Android中文乱码彻底解决
http://www.iteye.com/topic/509046
Android 中下载文件到sdcard和进度条小结
http://www.linuxidc.com/Linux/2011-08/40941.htm
Android中的自动下载歌词
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=61595
[2] 公共视图的兑现
来源: 互联网 发布时间: 2014-02-18
公共视图的实现
如上图所示,页面分为两个部分:上半部分是一个UIDatePicker,下半部分是一个UITableView。这两部分可以同时放在同一个XIB里,但是,如果UITableView是一个公共视图的话,也就是说,其他页面也会显示这个UITableView,而且列表内容相同,那么,这样做的话,就得在每个需要显示该表视图的XIB中都加上UITableView以及相应的实现,这样就造成了冗余。
我们可以将这个表视图单独出来,作为公共的部分,在需要显示的地方加载进来即可。
下面看一个简单的示例。
新建一个Test项目,TestAppDelegate.h内容如下:
#import <UIKit/UIKit.h>
@class TestViewController;
@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
TestViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet TestViewController *viewController;
@end
TestAppDelegate.m内容如下:
#import "TestAppDelegate.h"
#import "TestViewController.h"
@implementation TestAppDelegate
@synthesize window;
@synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
TestViewController.xib中是一个UIDatePicker,TestViewController.h内容如下:
#import <UIKit/UIKit.h>
#import "tableViewController.h"
@interface TestViewController : UIViewController {
tableViewController *tblController;
}
@end
TestViewController.m内容如下:
#import "TestViewController.h"
#import "tableViewController.h"
@implementation TestViewController
- (void)viewDidLoad {
[super viewDidLoad];
tblController = [[tableViewController alloc] init];
CGRect rect = CGRectMake(0, 220.f, 320.f, 230.f);
tblController.view.frame = rect;
[self.view addSubview:tblController.view];
}
- (void)dealloc {
[tblController release];
[super dealloc];
}
@end
tableViewController.xib中是一个UITableView,tableViewController.h内容如下:
#import <UIKit/UIKit.h>
@interface tableViewController : UITableViewController {
}
@property(nonatomic, retain) NSArray *ary;
@end
tableViewController.m内容如下:
#import "tableViewController.h"
@implementation tableViewController
@synthesize ary;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", nil];
self.ary = array;
[array release];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.ary count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.ary objectAtIndex:[indexPath row]];
return cell;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[ary release];
[super dealloc];
}
@end
[3] Objective-C Unicode 转换成汉语【转】
来源: 互联网 发布时间: 2014-02-18
Objective-C Unicode 转换成中文【转】
+ (NSString *)replaceUnicode:(NSString *)unicodeStr {
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
//NSLog(@"Output = %@", returnStr);
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
最新技术文章: