I am now making a drawing app where user can import pictures from gallery and further draw on it. Pictures can be loaded successfully and things could be drawn successfully, but i would like to further modify because when the imported files dimension is greater than the screen size, it will only show part of the image.
Hence i load the screen dimension and the importing file demension and if certain conditions, it will popup the alertdialog such that the user can either choose to stetch or to import that pic by filling up the maximum width or height, keeping the width:height scale.
For loading the picture, it is in ActivityA calling a method in ActivityB (name as DrawView).
Question:
The alertdialog underlines an error The constructor AlertDialog.Builder(DrawView) is undefined at the phrase
new AlertDialog.Builder(this);
I have tried modified the above as
new AlertDialog.Builder(DrawView.this);
but still fails. How could this be modified? Many thanks!!!
Coding:
public void load_pic(String picturePath) // load a picture from gallery
{
// get screen dimension first
WindowManager wm = (WindowManager) context_new.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
Options op = new Options();
op.inJustDecodeBounds = true;
Bitmap pic_to_be_imported = BitmapFactory.decodeFile(picturePath, op);
int x_pic_to_be_imported = op.outWidth;
int y_pic_to_be_imported = op.outHeight;
if ((x_pic_to_be_imported > screenWidth) || (y_pic_to_be_imported > screenHeight))
{
TextView myView2 = new TextView(context_new.getApplicationContext());
myView2.setText(R.string.message_load_pic);
myView2.setTextSize(15);
AlertDialog.Builder onBackBuilder = new AlertDialog.Builder(DoodleView.this);
onBackBuilder.setTitle(R.string.menuitem_on_load_pic);
onBackBuilder.setView(myView2);
onBackBuilder.setCancelable(true);
onBackBuilder.setPositiveButton(R.string.buttontext_create_load_pic_extend, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//code
}
});
onBackBuilder.setNegativeButton(R.string.buttontext_create_load_pic_keep_scale, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
bitmap = (BitmapFactory.decodeFile(picturePath)).copy(Bitmap.Config.ARGB_8888, true)
.createScaledBitmap(bitmap, screenWidth, screenHeight, true);
bitmapCanvas = new Canvas(bitmap);
}
});
AlertDialog alert = onBackBuilder.create();
alert.show();
}
// further codes
}
pass Activity context for creating AlertDialog instead of non Activity Class as:
where
context_newis Activity context which you are passing toDoodleViewat the time of object creation in Activity