Question

I access the android gallery and select image. after view photo on image view ..

this code is true work.

   targetImage = (ImageView)findViewById(R.id.imageView1);
   public void Onclickimage(View view){
    try{

        Intent intent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 0);
    }
    catch (Exception ex)
    {
        Toast.makeText(this,ex.getMessage(),Toast.LENGTH_LONG).show();
    }
}
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();
        targetImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
     }

I would like another buttonclick set text on photo to imageview. This code does not work.

         public void onClick(View view) {
             Bitmap bmp = drawTextToBitmap(MainActivity.this,R.id.imageView1,"MyText"); 
             targetImage.setImageBitmap(bmp);
         }
         public Bitmap drawTextToBitmap(Context mContext,  int resourceId,  String mText) {

    try {
            Resources resources = mContext.getResources();
                float scale = resources.getDisplayMetrics().density;
            ImageView img = (ImageView)findViewById(R.id.imageView1);
            float xson = img.getWidth();
            float yson = img.getHeight();
                Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
            android.graphics.Bitmap.Config bitmapConfig =   bitmap.getConfig();
            // set default bitmap config if none
            if(bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
            }
            // resource bitmaps are imutable,
            // so we need to convert it to mutable one
            bitmap = bitmap.copy(bitmapConfig, true);
            Canvas canvas = new Canvas(bitmap);
            // new antialised Paint
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            // text color - #3D3D3D
            paint.setColor(Color.BLACK);
            // text size in pixels
            paint.setTextSize((int) (12 * scale));
            // text shadow
            paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
            // draw text to the Canvas center
                Rect bounds = new Rect();
            paint.getTextBounds(mText, 0, mText.length(), bounds);
            int x = (bitmap.getWidth() - bounds.width())/4;
            int y = ((bitmap.getHeight() + bounds.height())/3)+50;
            paint.setColor(Color.RED);
                canvas.drawRect(x * scale-15, y * scale-30,xson+15 - x * scale , yson, paint); // for background
            paint.setColor(Color.BLACK); for text
            canvas.drawText(mText, x * scale, y * scale, paint);


            return bitmap;
    } catch (Exception e) {

        Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_LONG).show();

        return null;
    }

I write R.id.imageview1 instead to R.drawable.recimage this code is work but for just one photo.

Was it helpful?

Solution

I solve the problem

this code is wrong

Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

true code

img.setDrawingCacheEnabled(true);
Bitmap bitmap = img.getDrawingCache();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top