1

Android アプリの画像に角丸を追加しようとしています。角を丸くするために使用しているコードは次のとおりです。

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

    final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
    final SeekBar cornerRadius = (SeekBar) findViewById(R.id.seekBar1);
    final TextView tip = (TextView) findViewById(R.id.tip);

    bm = getRoundedCornerBitmap(BitmapFactory.decodeFile(picturePath),
            cornerRadius.getProgress());
     imageView.setImageBitmap(bm);

上記の角の半径は、シークバーの値から取得されます。

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = pixels;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

保存ボタンをクリックして呼び出している写真をSDカードに保存するためのコードは次のとおりです。

        OutputStream fOut = null;
        File file = new File(picturePath);
        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
        bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
                .parse("file://"
                        + Environment.getExternalStorageDirectory())));

        Toast.makeText(MainActivity.this,
                "Pic save successfuly. Path : " + picturePath,
                Toast.LENGTH_SHORT).show();

私が直面している問題は、画像が編集されて角が丸くなることです。次回、同じ画像を開いて、別のコーナー半径値で保存しようとすると、画像が保存されません。上記のトーストメッセージを2回目に受け取りますが、画像は変更されません。

4

1 に答える 1

0

動作しました。読み取りと書き込みのアクセス許可が台無しになりました。

于 2013-08-21T09:13:18.367 に答える