我的清单中有这些许可。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
但我使用的是目标 SDK 版本 15。是否要求您必须使用目标 SDK 18?
顺便提一句:
以下是从 Facebook 下载个人资料图片的示例代码:
private class DownloadProfilePicTask extends AsyncTask<Void,String,String> {
Bitmap profilePic;
String fileName;
String id;
String type;
URL img_value;
public DownloadProfilePicTask(String i,String ty)
{
id = i;
if(id==null)
{
//Log.v("Id is null", "Error");
}
//Log.v("Download Profile Pic Task initialized for id:",id);
type = ty;
}
@Override
protected String doInBackground(Void...param) {
String root = Environment.getExternalStorageDirectory().toString();
if(root==null)
{
return null;
}
try{
profilePic = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
}
catch (IOException e) {
e.printStackTrace();
}
if(profilePic == null)
{
//Log.v("profilePic is null", "Error");
}
//Log.v("Root for saving images",root );
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
fileName = root + "/saved_images/" + id + ".png";
//Log.v("filename is ",fileName);
File file = new File (fileName);
fileName = file.getPath();
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
profilePic.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return id;
}
@Override
protected void onPreExecute()
{
try
{
img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=" + type);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Override
protected void onPostExecute(String result) {
}
}
然后我就打电话:
new DownloadProfilePicTask(id,type).execute();
下载并自动保存图像。
注意:您将不得不稍微使用 filePath 来确定确切的位置。