Permalink
Browse files

fix importing image from gallery

  • Loading branch information...
1 parent 8a49878 commit d05168233e13dca6652d8005af49cc278d18a32d @kazuyayokoyama committed May 3, 2012
View
@@ -6,6 +6,7 @@
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+ <uses-permission android:name="android.permission.INTERNET" />
<application
android:label="@string/app_name"
@@ -18,7 +19,6 @@
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
<category android:name="musubi.intent.category.MENU" />
<category android:name="musubi.intent.category.DEFAULT" />
</intent-filter>
@@ -18,6 +18,9 @@
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
import java.util.UUID;
import leoliang.tasks365.DraggableListView;
@@ -31,16 +34,19 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
+import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
+import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
+import android.provider.MediaStore.MediaColumns;
import android.support.v4.app.ListFragment;
import android.support.v4.app.SupportActivity;
import android.support.v4.view.MenuItem;
@@ -239,11 +245,9 @@ private void goCamera() {
}
private void goGallery() {
- File tmpFile = JpgFileHelper.getTmpFile();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tmpFile));
startActivityForResult(intent, REQUEST_GALLERY);
}
@@ -372,17 +376,59 @@ protected void onPostExecute(Boolean result) {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
+ if (DEBUG) Log.d(TAG, "requestCode: " + requestCode + ", resultCode: " + resultCode);
if (resultCode == Activity.RESULT_OK) {
try {
- // uncomment below if you want to get small image
- // Bitmap b = (Bitmap)data.getExtras().get("data");
+ File imageFile = null;
+
+ if (requestCode == REQUEST_IMAGE_CAPTURE) {
+
+ imageFile = JpgFileHelper.getTmpFile();
+
+ } else if (requestCode == REQUEST_GALLERY) {
+ Uri uri = data.getData();
+ if (uri == null || uri.toString().length() == 0) {
+ return;
+ }
- File tmpFile = JpgFileHelper.getTmpFile();
- if (tmpFile.exists() && tmpFile.length() > 0) {
+ if (DEBUG) Log.d(TAG, "data URI: " + uri.toString());
+
+ ContentResolver cr = getActivity().getContentResolver();
+ String[] columns = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
+ Cursor c = cr.query(uri, columns, null, null, null);
+
+ if (c != null && c.moveToFirst()) {
+ if (c.getString(0) != null) {
+ //regular processing for gallery files
+ imageFile = new File(c.getString(0));
+ } else {
+ final InputStream is = getActivity().getContentResolver().openInputStream(uri);
+ imageFile = JpgFileHelper.saveTmpFile(is);
+ is.close();
+ }
+ } else {
+ // http or https
+ HttpURLConnection http = null;
+ URL url = new URL(uri.toString());
+ http = (HttpURLConnection)url.openConnection();
+ http.setRequestMethod("GET");
+ http.connect();
+
+ final InputStream is = http.getInputStream();
+ imageFile = JpgFileHelper.saveTmpFile(is);
+ is.close();
+ if (http != null) http.disconnect();
+ }
+ }
+
+ if (imageFile.exists() && imageFile.length() > 0) {
+ if (DEBUG) Log.d(TAG, "imageFile exists=" + imageFile.exists()
+ + " length=" + imageFile.length() + " path=" + imageFile.getPath());
+
float degrees = 0;
try {
ExifInterface exif = new ExifInterface(
- tmpFile.getPath());
+ imageFile.getPath());
switch (exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL)) {
@@ -403,7 +449,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
e.printStackTrace();
}
- Bitmap bitmap = BitmapHelper.getResizedBitmap(tmpFile,
+ Bitmap bitmap = BitmapHelper.getResizedBitmap(imageFile,
BitmapHelper.MAX_IMAGE_WIDTH,
BitmapHelper.MAX_IMAGE_HEIGHT, degrees);
@@ -426,7 +472,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
refreshView();
- tmpFile.delete();
+ JpgFileHelper.deleteTmpFile();
}
} catch (Exception e) {
@@ -20,6 +20,7 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import android.graphics.Bitmap;
@@ -67,4 +68,25 @@ public static File getTmpFile() {
}
return new File(tmpImageFile);
}
+
+ public static File saveTmpFile(InputStream is) throws Exception {
+ File file = getTmpFile();
+ OutputStream os = new FileOutputStream(file);
+
+ byte[] buf = new byte[1024];
+ int len = 0;
+
+ while((len = is.read(buf)) > 0){
+ os.write(buf, 0, len);
+ }
+
+ os.flush();
+ os.close();
+
+ return file;
+ }
+
+ public static boolean deleteTmpFile() {
+ return deleteJpegFile("tmp");
+ }
}

0 comments on commit d051682

Please sign in to comment.