如何保存android ImageView中显示的图像?

如何保存android ImageView中显示的图像?,android,Android,我的要求是, 加载可绘制文件夹的图像,然后在图像周围添加圆角 在图像周围填充白色/绿色笔划 这里我完成了这两个步骤,我的问题是,当我存储图像视图中显示的图像时,它没有存储 对于圆角,我使用了下面的代码 private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) { Bitmap bitmap = scaledBitmap.getBitmap(); result = Bi

我的要求是,

  • 加载可绘制文件夹的图像,然后在图像周围添加圆角
  • 在图像周围填充白色/绿色笔划
  • 这里我完成了这两个步骤,我的问题是,当我存储图像视图中显示的图像时,它没有存储

    对于圆角,我使用了下面的代码

       private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {
    
            Bitmap bitmap = scaledBitmap.getBitmap();
    
            result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                    Bitmap.Config.ARGB_8888);
            canvas = new Canvas(result);
    
            color = 0xff424242;
            paint = new Paint();
            rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            rectF = new RectF(rect);
            roundPx = i;
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
    
                   //Add Stroke code Snippet here
    
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
            BitmapDrawable finalresult = new BitmapDrawable(result);
            saveImage_Rotate_white(finalresult.getBitmap());
            return finalresult;
        }
    
    在mymain.xml中:

         <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/finalLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/black"
        android:orientation="vertical" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/roundcorner"
            android:src="@drawable/test" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:text="Click here to Rotate" />
    
    </RelativeLayout>
    
    我的整个活动是

    public class BorderDraw extends Activity {
        ImageView rec_image;
        String rec_path;
        RelativeLayout mLayout;
        private Bitmap result;
        private int color;
        private Paint paint;
        private Rect rect;
        private RectF rectF;
        private Canvas canvas;
        private float roundPx;
        RelativeLayout innerLayout;
        static File rotated_File, bordered_file;
        Button b1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            b1 = (Button) findViewById(R.id.button1);
            b1.setVisibility(View.GONE);
            mLayout = (RelativeLayout) findViewById(R.id.finalLayout);
            rec_image = (ImageView) findViewById(R.id.imageView1);
            rec_path = getIntent().getExtras().getString("image_file");
            Log.v("Image Path", rec_path);
            // rec_image.setDrawingCacheEnabled(true);
            String fname = "RoundCorner.jpg";
            rotated_File = new File("/sdcard/" + fname);
            bordered_file = new File("/sdcard/cornered_image.png");
            BitmapDrawable scaledBitmap = resizeBitmap(rec_path, 200, 200,
                    rec_image);
    
            BitmapDrawable cornered_bitmap = roundCornered(scaledBitmap, 12);
    
            // saveImage_Rotate_white(cornered_bitmap.getBitmap());
            rec_image.setImageDrawable(cornered_bitmap);
    
            System.out.println("ImageView Height" + rec_image.getHeight());
            System.out.println("ImageView Width" + rec_image.getWidth());
    
        }
    
        private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {
    
            Bitmap bitmap = scaledBitmap.getBitmap();
    
            result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                    Bitmap.Config.ARGB_8888);
            canvas = new Canvas(result);
    
            color = 0xff424242;
            paint = new Paint();
            rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            rectF = new RectF(rect);
            roundPx = i;
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
    
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            BitmapDrawable finalresult = new BitmapDrawable(result);
            saveImage_Rotate_white(finalresult.getBitmap());
            return finalresult;
        }
    
        private void saveImage_Rotate_white(Bitmap bitmap) {
            rec_image.setImageBitmap(bitmap);
            final Bitmap myRoundedImage = rec_image.getDrawingCache();
    
            if (bordered_file.exists())
                bordered_file.delete();
            try {
                FileOutputStream out = new FileOutputStream(bordered_file);
                myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        private BitmapDrawable resizeBitmap(String rec_path2, int wantedWidth,
                int wantedHeight, ImageView view) {
            // TODO Auto-generated method stub
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inTempStorage = new byte[16 * 1024];
    
            Bitmap rec_bitmap = BitmapFactory.decodeFile(rec_path2, options);
    
            int width = rec_bitmap.getWidth();
            int height = rec_bitmap.getHeight();
            float xScale = ((float) wantedWidth) / width;
            float yScale = ((float) wantedHeight) / height;
            float scale = (xScale <= yScale) ? xScale : yScale;
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
            Bitmap scaledBitmap = Bitmap.createBitmap(rec_bitmap, 0, 0, width,
                    height, matrix, true);
            BitmapDrawable finalImage = new BitmapDrawable(scaledBitmap);
            width = scaledBitmap.getWidth();
            height = scaledBitmap.getHeight();
    
            return finalImage;
    
        }
    
        static void saveImage_Rotate(Bitmap dest2) {
    
            if (rotated_File.exists())
                rotated_File.delete();
            try {
                FileOutputStream out = new FileOutputStream(rotated_File);
                dest2.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    
    public类BorderDraw扩展活动{
    ImageView rec_图像;
    字符串记录路径;
    相对布局;
    私有位图结果;
    私人内特色;
    私人油漆;
    私人直肠;
    私有RectF-RectF;
    私人帆布;
    私人浮动轮;
    相对的内部布局;
    静态文件旋转_文件,带边框_文件;
    按钮b1;
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    //TODO自动生成的方法存根
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1=(按钮)findViewById(R.id.button1);
    b1.设置可见性(视图已消失);
    mLayout=(相对长度)findViewById(R.id.finalLayout);
    rec_image=(ImageView)findViewById(R.id.imageView1);
    rec_path=getIntent().getExtras().getString(“图像文件”);
    Log.v(“图像路径”,rec_路径);
    //rec_image.setDrawingCacheEnabled(真);
    String fname=“RoundCorner.jpg”;
    旋转的文件=新文件(“/sdcard/”+fname);
    bordered_file=新文件(“/sdcard/corneed_image.png”);
    BitmapDrawable scaledBitmap=resizeBitmap(记录路径,200200,
    记录图像);
    BitmapDrawable cornered_位图=圆角(缩放位图,12);
    //saveImage_Rotate_white(转角_bitmap.getBitmap());
    rec_image.setImageDrawable(角落位图);
    System.out.println(“ImageView高度”+rec_image.getHeight());
    System.out.println(“ImageView宽度”+rec_image.getWidth());
    }
    私有位图可绘制圆角(位图可绘制缩放位图,int i){
    位图位图=缩放位图.getBitmap();
    结果=Bitmap.createBitmap(Bitmap.getWidth()、Bitmap.getHeight(),
    位图.Config.ARGB_8888);
    画布=新画布(结果);
    颜色=0xFF4242;
    油漆=新油漆();
    rect=newrect(0,0,bitmap.getWidth(),bitmap.getHeight());
    rectF=新的rectF(rect);
    roundPx=i;
    paint.setAntiAlias(真);
    drawARGB(0,0,0,0);
    canvas.drawRoundRect(rectF、roundPx、roundPx、paint);
    setXfermode(新的PorterDuffXfermode(Mode.SRC_IN));
    绘制位图(位图、矩形、矩形、绘制);
    BitmapDrawable finalresult=新的BitmapDrawable(结果);
    saveImage_Rotate_white(finalresult.getBitmap());
    返回最终结果;
    }
    私有void保存图像\旋转\白色(位图){
    rec_image.setImageBitmap(位图);
    最终位图myRoundedImage=rec_image.getDrawingCache();
    if(带边框的_文件.exists())
    带边框的_文件。删除();
    试一试{
    FileOutputStream out=新的FileOutputStream(带边框的_文件);
    myRoundedImage.compress(Bitmap.CompressFormat.PNG,90,out);
    out.flush();
    out.close();
    }捕获(例外e){
    e、 printStackTrace();
    }
    }
    私有位图可缩放位图(字符串rec_path2,int wantedWidth,
    int wantedHeight,ImageView){
    //TODO自动生成的方法存根
    BitmapFactory.Options=new-BitmapFactory.Options();
    options.inTempStorage=新字节[16*1024];
    位图rec_Bitmap=BitmapFactory.decodeFile(rec_路径2,选项);
    int width=rec_bitmap.getWidth();
    int height=rec_bitmap.getHeight();
    浮动xScale=((浮动)wantedWidth)/宽度;
    浮动Y刻度=((浮动)所需高度)/高度;
    
    float scale=(xScale您可以实现的一件事是: 在您的
    活动
    片段
    ,或在任何您正在膨胀
    XML的地方,为您的
    视图
    启用缓存,如下所示:

    ImageView myImageView= (ImageView)findViewById(R.id.imageView1);
    myImageView.setDrawingCacheEnabled(true);
    
    final Bitmap myRoundedImage = myImageView.getDrawingCache();
    
    每当您想要检索位图时,只需调用get drawing缓存,如下所示:

    ImageView myImageView= (ImageView)findViewById(R.id.imageView1);
    myImageView.setDrawingCacheEnabled(true);
    
    final Bitmap myRoundedImage = myImageView.getDrawingCache();
    
    或者,您可以执行以下操作:

    final Bitmap myRoundedImage = Bitmap.createBitmap( 
        myImageView.getWidth(), myImageView.getHeight(), Bitmap.Config.ARGB_8888 ); 
    final Canvas canvas = new Canvas( myRoundedImage ); 
    myImageView.draw( canvas );
    
    一旦你有了位图,你所要做的就是将它保存到SD卡上,比如:

    try {
           FileOutputStream out = new FileOutputStream(filename);
           myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
           e.printStackTrace();
    }
    

    您可以实现的一件事是: 在您的
    活动
    片段
    ,或在任何您正在膨胀
    XML的地方,为您的
    视图
    启用缓存,如下所示:

    ImageView myImageView= (ImageView)findViewById(R.id.imageView1);
    myImageView.setDrawingCacheEnabled(true);
    
    final Bitmap myRoundedImage = myImageView.getDrawingCache();
    
    每当您想要检索位图时,只需调用get drawing缓存,如下所示:

    ImageView myImageView= (ImageView)findViewById(R.id.imageView1);
    myImageView.setDrawingCacheEnabled(true);
    
    final Bitmap myRoundedImage = myImageView.getDrawingCache();
    
    或者,您可以执行以下操作:

    final Bitmap myRoundedImage = Bitmap.createBitmap( 
        myImageView.getWidth(), myImageView.getHeight(), Bitmap.Config.ARGB_8888 ); 
    final Canvas canvas = new Canvas( myRoundedImage ); 
    myImageView.draw( canvas );
    
    一旦你有了位图,你所要做的就是将它保存到SD卡上,比如:

    try {
           FileOutputStream out = new FileOutputStream(filename);
           myRoundedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
    } catch (Exception e) {
           e.printStackTrace();
    }
    

    所以你的问题是如何在SD卡中以圆形边界存储图像???永远不要直接访问SD卡。使用环境这里是一个例子:它已保存,但我无法查看图像,当我打开它时,它说,无法打开此图像。所以你的问题是如何在SD卡中以圆形边界存储图像???永远不要直接访问SD卡。使用Envir这里的onment是一个示例:它已保存但无法查看图像,当我打开它时,显示无法打开此图像。我使用了两种方法,但无法获得解决方案。当我使用此最终位图MyRoundeImage=myImageView.getDrawingCache()时,图像将无法创建。设备显示“无法加载此图像”以及当我使用最终位图MyRoundeImage=Bitmap.createBitmap(myImageView.getWidth(),myImageView.getHeight(),Bitmap.Config.ARGB_8888);最终画布=新画布(MyRoundeImage);myImageView.draw(画布);表示它在这一行上抛出错误。要编辑您的代码,我需要更多的代码。例如,您的活动或片段,或者无论您在何处声明I