-
Notifications
You must be signed in to change notification settings - Fork 32
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/src/main/java/com/github/dfa/diaspora_android/service/SaveImageTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
+ 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructor (Context context) |
||
saveTask.execute(url); | ||
} | ||
} | ||
break; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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");