mirror of
https://github.com/gsantner/dandelion
synced 2024-11-16 09:22:08 +01:00
Fixed faulty extensions on downloaded images
This commit is contained in:
parent
bea35292f8
commit
851b223733
2 changed files with 86 additions and 11 deletions
|
@ -0,0 +1,74 @@
|
||||||
|
package com.github.dfa.diaspora_android.service;
|
||||||
|
|
||||||
|
import android.app.DownloadManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.os.Looper;
|
||||||
|
import android.os.StrictMode;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.github.dfa.diaspora_android.R;
|
||||||
|
import com.github.dfa.diaspora_android.util.AppLog;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by vanitas on 06.11.16.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SaveImageTask extends AsyncTask<String, Void, String> {
|
||||||
|
|
||||||
|
protected Context context;
|
||||||
|
|
||||||
|
public void setContext(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String doInBackground(String... urls) {
|
||||||
|
String url = urls[0];
|
||||||
|
if (url != null) {
|
||||||
|
Uri source = Uri.parse(url);
|
||||||
|
DownloadManager.Request request = new DownloadManager.Request(source);
|
||||||
|
URL sourceUrl;
|
||||||
|
InputStream is;
|
||||||
|
byte[] a = new byte[8];
|
||||||
|
String extension = ".png";
|
||||||
|
try {
|
||||||
|
sourceUrl = new URL(source.toString());
|
||||||
|
is = sourceUrl.openStream();
|
||||||
|
is.read(a);
|
||||||
|
is.close();
|
||||||
|
AppLog.d(this, "Array: " + new String(a));
|
||||||
|
//JPG
|
||||||
|
if (new String(a).startsWith(new String(new byte[]{-1, -40}))) {
|
||||||
|
AppLog.d(this, "is jpg");
|
||||||
|
extension = ".jpg";
|
||||||
|
} else
|
||||||
|
//GIF
|
||||||
|
if (new String(a).startsWith("GIF")) {
|
||||||
|
AppLog.d(this, "is gif");
|
||||||
|
extension = ".gif";
|
||||||
|
} else {
|
||||||
|
AppLog.d(this, "is SPARTAAAA! (GIF)");
|
||||||
|
}
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
File destinationFile = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/"
|
||||||
|
+ System.currentTimeMillis() + extension);
|
||||||
|
request.setDestinationUri(Uri.fromFile(destinationFile));
|
||||||
|
((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request);
|
||||||
|
return destinationFile.getAbsolutePath();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String s) {
|
||||||
|
Toast.makeText(context, context.getString(R.string.share__toast_saved_image_to_location)+" "+s, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,18 +30,27 @@ import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
import android.os.StrictMode;
|
||||||
import android.support.v4.content.LocalBroadcastManager;
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.view.ContextMenu;
|
import android.view.ContextMenu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.github.dfa.diaspora_android.App;
|
||||||
import com.github.dfa.diaspora_android.R;
|
import com.github.dfa.diaspora_android.R;
|
||||||
import com.github.dfa.diaspora_android.activity.MainActivity;
|
import com.github.dfa.diaspora_android.activity.MainActivity;
|
||||||
import com.github.dfa.diaspora_android.service.ImageDownloadTask;
|
import com.github.dfa.diaspora_android.service.ImageDownloadTask;
|
||||||
|
import com.github.dfa.diaspora_android.service.SaveImageTask;
|
||||||
|
import com.github.dfa.diaspora_android.util.AppLog;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subclass of WebView which adds a context menu for long clicks on images or links to share, save
|
* Subclass of WebView which adds a context menu for long clicks on images or links to share, save
|
||||||
|
@ -107,17 +116,9 @@ public class ContextMenuWebView extends NestedWebView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (writeToStoragePermitted) {
|
if (writeToStoragePermitted) {
|
||||||
if (url != null) {
|
SaveImageTask saveTask = new SaveImageTask();
|
||||||
Uri source = Uri.parse(url);
|
saveTask.setContext(context);
|
||||||
DownloadManager.Request request = new DownloadManager.Request(source);
|
saveTask.execute(url);
|
||||||
File destinationFile = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/"
|
|
||||||
+ System.currentTimeMillis() + ".png");
|
|
||||||
request.setDestinationUri(Uri.fromFile(destinationFile));
|
|
||||||
((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request);
|
|
||||||
|
|
||||||
Toast.makeText(context, context.getText(R.string.share__toast_saved_image_to_location) + " " +
|
|
||||||
destinationFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue