我正在使用此代码将图像保存到文件夹中。
File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/" + getFilenameFromURL(imageUrl));
if(!file.exists()) file.mkdirs();
但是还创建了一个额外的文件夹,其名称与图像名称相同......
答案 0 :(得分:1)
试试这个:
File RootFile;
File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/MyAppFolder/");
String title=" <your title>";
if (!dir.exists()) {
dir.mkdirs();
}
RootFile = new File(dir, title);
答案 1 :(得分:0)
You must declare permissions in AndoridManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
//Call this method on image loading in your yourActivity.java
if (NetConnectivity.isOnline(getApplicationContext())) {
//define String link,filename varialble as global variables in activity
link = "Replace Remote URL";
filename = "Replace your new name of image";
//Here you can check folder is exist or not, if not exist then folder has been created
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "YourFolderName");
folder.mkdir();
Log.d("File New Name : ", System.currentTimeMillis() + filename);
Log.d("Folder PAth : ", folder.toString());
File file = new File(folder, System.currentTimeMillis() + filename);
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
//DownloadFile("http://www.nmu.ac.in/ejournals/aspx/courselist.pdf", file);
new DownloadFileFromURL().execute(link);
} else {
Toast.makeText(getApplicationContext(), getString(R.string.no_network2), Toast.LENGTH_SHORT).show();
}
//DownloadFileFromURL Async Task ,Copy this method outside OnCreate() Method
class DownloadFileFromURL extends AsyncTask<String, String, String> {
private ProgressDialog pDialogkk;
/**
* Before starting background thread Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialogkk = new ProgressDialog(FilesInDownloadsActivity.this);
pDialogkk.setMessage("Downloading file. Please wait...");
pDialogkk.setIndeterminate(false);
pDialogkk.setMax(100);
pDialogkk.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialogkk.setCancelable(true);
pDialogkk.show();
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
if (url.equals("") || url.equals(null)) {
Toast.makeText(getApplicationContext(), "Not Found",
Toast.LENGTH_SHORT).show();
} else {
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(
url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/" + getString(R.string.app_name) + "/"
+ filename);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""
+ (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialogkk.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
pDialogkk.dismiss();
Toast.makeText(context,
"File Has been Downloaded,Check in your " + "/sdcard/" + getString(R.string.app_name) + "/",
Toast.LENGTH_LONG).show();
// Displaying downloaded image into image view
// Reading image path from sdcard
// String imagePath = Environment.getExternalStorageDirectory()
// .toString() + "/downloadedfile.jpg";
// setting downloaded into image view
}
}