如何将图像编辑到android库并保存

时间:2014-03-25 00:00:41

标签: imageview gallery edit photo

我访问android库并选择图片。在图像视图上查看照片后..

这段代码是真实的工作。

   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));
     }

我想在照片上另一个按钮点击设置文字到imageview。 此代码不起作用。

         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;
    }

我将R.id.imageview1改为R.drawable.recimage,这段代码可以正常使用,但仅限一张照片。

1 个答案:

答案 0 :(得分:0)

我解决了问题

此代码错误

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

真实代码

img.setDrawingCacheEnabled(true);
Bitmap bitmap = img.getDrawingCache();