Bug 990642 - Try harder to find an extension to use when sharing images. r=mfinkle

This commit is contained in:
Wes Johnston 2014-05-12 10:13:45 -07:00
Родитель 6e08f524ef
Коммит 2ffe81dd9f
2 изменённых файлов: 23 добавлений и 5 удалений

Просмотреть файл

@ -109,6 +109,7 @@ import android.view.TextureView;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import android.webkit.URLUtil;
import android.widget.AbsoluteLayout; import android.widget.AbsoluteLayout;
import android.widget.Toast; import android.widget.Toast;
@ -2657,37 +2658,53 @@ public class GeckoAppShell
GeckoApp.deleteTempFiles(); GeckoApp.deleteTempFiles();
String type = intent.getType();
OutputStream os = null; OutputStream os = null;
try { try {
// Create a temporary file for the image // Create a temporary file for the image
final String type = intent.getType().replace("image/", "");
final File imageFile = File.createTempFile("image", "." + type, dir);
os = new FileOutputStream(imageFile);
if (src.startsWith("data:")) { if (src.startsWith("data:")) {
int dataStart = src.indexOf(","); final int dataStart = src.indexOf(",");
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
// If we weren't given an explicit mimetype, try to dig one out of the data uri.
if (TextUtils.isEmpty(extension) && dataStart > 5) {
type = src.substring(5, dataStart).replace(";base64", "");
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(type);
}
final File imageFile = File.createTempFile("image", "." + extension, dir);
os = new FileOutputStream(imageFile);
byte[] buf = Base64.decode(src.substring(dataStart + 1), Base64.DEFAULT); byte[] buf = Base64.decode(src.substring(dataStart + 1), Base64.DEFAULT);
os.write(buf); os.write(buf);
// Only alter the intent when we're sure everything has worked
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile)); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
} else { } else {
InputStream is = null; InputStream is = null;
try { try {
final byte[] buf = new byte[2048]; final byte[] buf = new byte[2048];
final URL url = new URL(src); final URL url = new URL(src);
final String filename = URLUtil.guessFileName(src, null, type);
is = url.openStream(); is = url.openStream();
final File imageFile = new File(dir, filename);
os = new FileOutputStream(imageFile);
int length; int length;
while ((length = is.read(buf)) != -1) { while ((length = is.read(buf)) != -1) {
os.write(buf, 0, length); os.write(buf, 0, length);
} }
// Only alter the intent when we're sure everything has worked
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile)); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
} finally { } finally {
safeStreamClose(is); safeStreamClose(is);
} }
} }
} catch(IOException ex) { } catch(IOException ex) {
// If something went wrong, we'll just leave the intent un-changed
} finally { } finally {
safeStreamClose(os); safeStreamClose(os);
} }

Просмотреть файл

@ -5,6 +5,7 @@
package org.mozilla.gecko.widget; package org.mozilla.gecko.widget;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.Telemetry; import org.mozilla.gecko.Telemetry;
import org.mozilla.gecko.TelemetryContract; import org.mozilla.gecko.TelemetryContract;
import org.mozilla.gecko.menu.MenuItemActionView; import org.mozilla.gecko.menu.MenuItemActionView;