我想从url下载图片并存储在sdcard的文件夹中。
如果文件夹不存在,请创建文件夹并保存。
但是它给出以下例外:
java.io.FileNotFoundException: /mnt/sdcard (Is a directory)
答案 0 :(得分:2)
信息很清楚。 / mnt / sdcard 是一个目录而不是一个文件。您需要创建写入非目录路径的 FileOutputStream 。
例如:
//Setting up cache directory to store the image
File cacheDir=new File(context.getCacheDir(),"cache_folder");
// Check if cache folder exists, otherwise create folder.
if(!cacheDir.exists())cacheDir.mkdirs();
// Setting up file to write the image to.
File f=new File(cacheDir, "img.png");
// Open InputStream to download the image.
InputStream is=new URL(url).openStream();
// Set up OutputStream to write data into image file.
OutputStream os = new FileOutputStream(f);
HelperUtil.CopyStream(is, os);
...
/**
* Copy all data from InputStream and write using OutputStream
* @param is InputStream
* @param os OutputStream
*/
public static void CopyStream(InputStream is, OutputStream os)
{
final int buffer_size=1024;
try
{
byte[] bytes=new byte[buffer_size];
for(;;)
{
int count=is.read(bytes, 0, buffer_size);
if(count==-1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
答案 1 :(得分:2)
尝试这样的事情
String image_URL="http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=http%3A%2F%2Fandroid-er.blogspot.com%2F";
String extStorageDirectory;
File file;
Bitmap bm;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
file=new File(android.os.Environment.getExternalStorageDirectory(),"Your FolderName");
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
}else{
file=YourActivity.this.getCacheDir();
}
if(!file.exists())
file.mkdirs();
extStorageDirectory+="Your FolderName/yourimagename.PNG";
File imageFile = new File(extStorageDirectory);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
if(bitmap!=null){
imageview.setImageBitmap(bitmap);
}else{
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm = LoadImage(image_URL, bmOptions);
imageview.setImageBitmap(bm);
OutputStream outStream = null;
file=new File(android.os.Environment.getExternalStorageDirectory(),"Your FolderName");
file=new File(extStorageDirectory, "Your FolderName/yourimagename.PNG");
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
其中方法为
private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}catch (Exception ex){
Log.e("error",ex.toString());
}
return inputStream;
}
答案 2 :(得分:1)
尝试保存图片时,您提供了错误的路径。你似乎使用“/ mnt / sdcard”,而它应该是“/mnt/sdcard/image.jpg”
答案 3 :(得分:1)
BitmapFactory.Options bmOptions;
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
bm = LoadImage(image_url, bmOptions);
extStorageDirectory = Environment.getExternalStorageDirectory()
.toString() + "/image_folder";
OutputStream outStream = null;
File wallpaperDirectory = new File(extStorageDirectory);
wallpaperDirectory.mkdirs();
File outputFile = new File(wallpaperDirectory, "image.PNG");
try {
outStream = new FileOutputStream(outputFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
答案 4 :(得分:1)
尝试这个,如果它存在,将返回外部存储器,否则它将返回手机存储器目录。构造函数将Context和要创建的文件夹的名称作为参数。并尝试这个链接,它是你需要的相当不错的东西。 Image Loader
public class FileCache {
private File cacheDir;
private String applicationDirectory = Config.applicationMainFolder;
public FileCache(Context context, String folderName){
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(context.getExternalFilesDir(null), applicationDirectory + folderName);
else
cacheDir = new File(context.getCacheDir(), applicationDirectory + folderName);
if(!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url){
String filename=String.valueOf(url.hashCode());
File f = new File(cacheDir, filename);
return f;
}
public void clear(){
File[] files=cacheDir.listFiles();
if(files==null)
return;
for(File f:files)
f.delete();
}
}
答案 5 :(得分:1)
使用此
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
File file=new File(Environment.getExternalStorageDirectory()+path);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, bytes);
byte b[] = bytes.toByteArray();
try
{
FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
fos.flush();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
路径将是sdcard中的文件夹,而位图是图像的对象