Scale bitmaps to prevent out of memory errors

pull/14/head
Sam Bosley 12 years ago
parent 9ad04c4fbc
commit 0448778d64

@ -106,6 +106,25 @@ public class AndroidUtilities {
}
}
/** Read a bitmap from the specified file, scaling if necessary
* Returns null if scaling failed after several tries */
public static Bitmap readScaledBitmap(String file) {
Bitmap bitmap = null;
int tries = 1;
BitmapFactory.Options opts = new BitmapFactory.Options();
while(bitmap == null && tries < 32) {
opts.inSampleSize = tries;
try {
bitmap = BitmapFactory.decodeFile(file, opts);
} catch (OutOfMemoryError e) {
// Too big
}
tries = tries * 2;
}
return bitmap;
}
/**
* Start the given intent, handling security exceptions if they arise
*

@ -5,15 +5,19 @@ import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.timsu.astrid.R;
import com.todoroo.aacenc.RecognizerApi;
@ -21,6 +25,7 @@ import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
@ -148,7 +153,26 @@ public class FilesControlSet extends PopupControlSet {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Show image
AlertDialog image = new AlertDialog.Builder(activity).create();
ImageView imageView = new ImageView(activity);
imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Bitmap bitmap = AndroidUtilities.readScaledBitmap(filePath);
if (bitmap == null) {
Toast.makeText(activity, R.string.file_err_memory, Toast.LENGTH_LONG);
return;
}
imageView.setImageBitmap(bitmap);
image.setView(imageView);
image.setButton(activity.getString(R.string.DLG_close), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
return;
}
});
image.show();
}
});
break;

@ -97,6 +97,8 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
private final int cameraButton;
private final String linkColor;
private boolean respondToPicture;
private final List<UpdatesChangedListener> listeners = new LinkedList<UpdatesChangedListener>();
public interface UpdatesChangedListener {
@ -232,6 +234,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
ActFmCameraModule.showPictureLauncher(fragment, clearImage);
else
ActFmCameraModule.showPictureLauncher(fragment, null);
respondToPicture = true;
}
});
if(!TextUtils.isEmpty(task.getValue(Task.NOTES))) {
@ -541,16 +544,21 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
*/
public boolean activityResult(int requestCode, int resultCode, Intent data) {
CameraResultCallback callback = new CameraResultCallback() {
@Override
public void handleCameraResult(Bitmap bitmap) {
pendingCommentPicture = bitmap;
pictureButton.setImageBitmap(pendingCommentPicture);
}
};
if (respondToPicture) {
respondToPicture = false;
CameraResultCallback callback = new CameraResultCallback() {
@Override
public void handleCameraResult(Bitmap bitmap) {
pendingCommentPicture = bitmap;
pictureButton.setImageBitmap(pendingCommentPicture);
}
};
return (ActFmCameraModule.activityResult((Activity)getContext(),
requestCode, resultCode, data, callback));
return (ActFmCameraModule.activityResult((Activity)getContext(),
requestCode, resultCode, data, callback));
} else {
return false;
}
}
}

@ -22,5 +22,6 @@
<string name="file_browser_err_permissions">Permissions error! Please make sure you have not blocked Astrid from accessing the SD card.</string>
<string name="file_add_picture">Attach a picture</string>
<string name="file_add_sdcard">Attach a file from your SD card</string>
<string name="file_err_memory">Image is too large to fit in memory</string>
<string name="file_err_copy">Error copying file for attachment</string>
</resources>

Loading…
Cancel
Save