Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] Fixing Image Downloads #114

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ public boolean onNavigationItemSelected(MenuItem item) {
snackbarNoInternet.show();
}
}
break;

case R.id.nav_activities: {
if (WebHelper.isOnline(MainActivity.this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ public void onReceive(Context context, Intent intent) {
setTitle(R.string.nav_mentions);
} else if (subUrl.startsWith(DiasporaUrlHelper.SUBURL_PUBLIC)) {
setTitle(R.string.public_);
} else if (urls.getManageContactsUrl().equals(url)) {
setTitle(R.string.contacts);
} else if (urls.getManageTagsUrl().equals(url)) {
setTitle(R.string.pref_title__manage_tags);
} else if (urls.isAspectUrl(url)) {
setTitle(urls.getAspectNameFromUrl(url, app));
} else if (urls.getReportsUrl().equals(url)) {
setTitle(R.string.nav_reports);
}
} else {
AppLog.spam(this, "onReceive()- Invalid url: " + url);
Expand Down
Original file line number Diff line number Diff line change
@@ -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/"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "dandelion");

+ 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.StrictMode;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.widget.Toast;

import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.R;
import com.github.dfa.diaspora_android.activity.MainActivity;
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.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
Expand Down Expand Up @@ -107,17 +116,9 @@ public void onClick(DialogInterface dialog, int which) {
}
}
if (writeToStoragePermitted) {
if (url != null) {
Uri source = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(source);
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();
}
SaveImageTask saveTask = new SaveImageTask();
saveTask.setContext(context);
Copy link
Owner

@gsantner gsantner Dec 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructor (Context context)

saveTask.execute(url);
}
}
break;
Expand Down