不能创建目录是怎么回事???

flying_vip_521 2011-08-09 07:07:16
写了一个图片工具类,不知道为什么不能在手机上创建目录,存在本地,请高手指点,谢谢!



package com.net.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class BitmapUtil {
private static final String TAG = "BtimapUtil";

private static BitmapUtil bitmapUtil = new BitmapUtil();

public static BitmapUtil getInstance(){
return bitmapUtil;
}

public Bitmap getBitmap(String url,Context context){
String imageName= url.substring(url.lastIndexOf("/")+1, url.length());
File file = new File(getSDPath(),imageName);
Log.e(TAG, "filepath="+file.getPath());
if(file.exists()){
Log.e(TAG, "getBitmap from Local");
return BitmapFactory.decodeFile(file.getPath());
}
return getNetBitmap(url,file,context);
}

public List<Bitmap> getBitmap(List<String> list,Context context){
List<Bitmap> result = new ArrayList<Bitmap>();
for(String strUrl : list){
Bitmap bitmap = getBitmap(strUrl,context);
if(bitmap!=null){
result.add(bitmap);
}
}
return result;
}

private String getSDPath(){
String path = null;
boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(hasSDCard){
path="/mnt/sdcard/innomarket/images/";
}else{
path="/data/data/com.innofidei.android.innomarket/cach/images/";
}
Log.e(TAG,"path="+path);
File file = new File(path);
if(!file.exists()){
file.mkdir();
Log.e(TAG,"no----file");
}
return file.getPath();
}

private Bitmap getNetBitmap(String strUrl,File file,Context context) {
Log.e(TAG, "getBitmap from net");
Bitmap bitmap = null;
if(NetUtil.isConnnected(context)){
try {
URL url = new URL(strUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.connect();
InputStream in = con.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
Log.e(TAG,file.getPath());
FileOutputStream out = new FileOutputStream(file.getPath());
bitmap.compress(Bitmap.CompressFormat.JPEG,100, out);

out.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}

}

...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
joze1122 2011-08-09
  • 打赏
  • 举报
回复
看你的路径好像是自己去拼写的,那你要清楚对于2.2 1.6不同平台路径是不同的,就拿SDCard的路径来说吧,不同平台路径分别是/sdcard或者/mnt/sdcard,所以你最好使用具体的函数得到路径

sdcard路径 Environment.getExternalStorageDirectory()
cache路径 context.getCacheDir()
xqhrs232 2011-08-09
  • 打赏
  • 举报
回复
没有权限?
ch_984326013 2011-08-09
  • 打赏
  • 举报
回复
有什么错误信息啊。贴出来看看!
flying_vip_521 2011-08-09
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.net.test"
android:versionCode="1"
android:versionName="1.0">


<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NetActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-sdk android:minSdkVersion="8" />
</manifest>

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧