|
|
|
@ -20,13 +20,17 @@ package org.floens.chan.utils; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import org.floens.chan.ChanApplication; |
|
|
|
|
import org.floens.chan.R; |
|
|
|
|
import org.floens.chan.core.ChanPreferences; |
|
|
|
|
import org.floens.chan.core.net.ByteArrayRequest; |
|
|
|
|
|
|
|
|
|
import android.app.AlertDialog; |
|
|
|
|
import android.app.DownloadManager; |
|
|
|
|
import android.content.Context; |
|
|
|
|
import android.content.DialogInterface; |
|
|
|
|
import android.content.Intent; |
|
|
|
|
import android.media.MediaScannerConnection; |
|
|
|
|
import android.net.Uri; |
|
|
|
@ -39,6 +43,43 @@ import com.android.volley.VolleyError; |
|
|
|
|
public class ImageSaver { |
|
|
|
|
private static final String TAG = "ImageSaver"; |
|
|
|
|
|
|
|
|
|
public static void saveAll(Context context, String folderName, final List<Uri> list) { |
|
|
|
|
final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); |
|
|
|
|
|
|
|
|
|
String folderPath = ChanPreferences.getImageSaveDirectory() + File.separator + folderName; |
|
|
|
|
File folder = Environment.getExternalStoragePublicDirectory(folderPath); |
|
|
|
|
int nextFileNameNumber = 0; |
|
|
|
|
while (folder.exists() || folder.isFile()) { |
|
|
|
|
folderPath = ChanPreferences.getImageSaveDirectory() + File.separator + folderName + "_" |
|
|
|
|
+ Integer.toString(nextFileNameNumber++); |
|
|
|
|
folder = Environment.getExternalStoragePublicDirectory(folderPath); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final String finalFolderPath = folderPath; |
|
|
|
|
String text = context.getString(R.string.download_confirm).replace("COUNT", Integer.toString(list.size())) |
|
|
|
|
.replace("FOLDER", folderPath); |
|
|
|
|
new AlertDialog.Builder(context).setMessage(text).setNegativeButton(R.string.cancel, null) |
|
|
|
|
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
|
|
|
|
@Override |
|
|
|
|
public void onClick(DialogInterface dialog, int which) { |
|
|
|
|
for (Uri uri : list) { |
|
|
|
|
DownloadManager.Request request = null; |
|
|
|
|
try { |
|
|
|
|
request = new DownloadManager.Request(uri); |
|
|
|
|
} catch (IllegalArgumentException e) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
request.setDestinationInExternalPublicDir(finalFolderPath, uri.getLastPathSegment()); |
|
|
|
|
request.setVisibleInDownloadsUi(false); |
|
|
|
|
request.allowScanningByMediaScanner(); |
|
|
|
|
|
|
|
|
|
dm.enqueue(request); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}).show(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void save(final Context context, String imageUrl, final String name, final String extension, |
|
|
|
|
final boolean share) { |
|
|
|
|
ChanApplication.getVolleyRequestQueue().add(new ByteArrayRequest(imageUrl, new Response.Listener<byte[]>() { |
|
|
|
|