I am new Android programming. I am trying to open an image from the Gallery of a phone and draw a Rectangle on top of the image I have opened. But I am able to open the image but I am not able to see the rectangle. I am using ImageView and Canvas to open image and draw. I have created a Button and used to image from Gallery. I have written the code to open and draw in onActiviesult() method.Could somebody help me?!. Thanks in advance

public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button gal=(Button)findViewById(R.id.button1); gal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent gal_open=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(gal_open,1); } }); } public void onActivityResult(int requestCode,int resultCode,Intent intentData){ super.onActivityResult(requestCode, resultCode, intentData); if(requestCode==1 && resultCode==RESULT_OK && intentData!=null){ ImageView img=(ImageView)findViewById(R.id.imageView1 ); Bitmap bmp=Bitmap.createBitmap(img.getHeight(),img.getWidth(),Bitmap.Config.RGB_565); Canvas cnvs=new Canvas(bmp); //img.setImageBitmap(bmp); Paint paint=new Paint(); paint.setColor(Color.RED); Uri data=intentData.getData(); String[] filePath={MediaStore.Images.Media.DATA}; Cursor cur=getContentResolver().query(data,filePath,null,null,null ); cur.moveToFirst(); int colIndex=cur.getColumnIndex(filePath[0]); String picPath=cur.getString(colIndex); cur.close(); cnvs.drawRect(20, 20,50,50 , paint); img.setImageBitmap(bmp); img.setImageBitmap(BitmapFactory.decodeFile(picPath)); } } return super.onOptionsItemSelected(item); } } 

2 Answers

try drawing the bitmap on the canvas and then call setImageBitmap just once. In this moment you are overriding the content of the ImageView. Remove img.setImageBitmap(BitmapFactory.decodeFile(picPath));

and

cnvs.drawBitmap(BitmapFactory.decodeFile(picPath), 0, 0, null); cnvs.drawRect(20, 20,50,50 , paint); img.setImageBitmap(bmp); 

I am not sure abaut the drawing order. If you don't see the rectangle try changing the order of drawRect and drawBitmap

0

You may try like this way which i use: create customborder.xml file.

Just take imageview in layout and make border xml file

and now use this customborder in layout backgourd like:

android:layout_height="fill_parent" android:background="@drawable/customborder"> 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy