1

我正在尝试将我的视图保存为图像。我做了一些工作,没有显示任何错误,但我可以找到保存图像的位置(或在图库中)。图像是完全创建的,还是我有其他问题?从选项菜单中按红色时应保存图像:

case R.id.red:
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date date = new Date();
                String FILENAME="Boenka";
                FileOutputStream fos = null;
                try {
                    fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                parent.setDrawingCacheEnabled(true);
                Bitmap  bitmap = Bitmap.createBitmap( parent.getWidth(), parent.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                parent.draw(canvas); 
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
                return true;

这是我的主要 - 绘图类

public class Draw extends Activity {
    DrawView drawView;
    SignatureView signature;
    private RelativeLayout parent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        parent = (RelativeLayout) findViewById(R.id.signImageParent);
        signature = new SignatureView(getApplicationContext(), null);
        signature.setColor(Color.MAGENTA);
        parent.addView(signature);

    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_options_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.clear:
            signature.clear();
            return true;

        case R.id.red:
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String FILENAME="Boenka";
            FileOutputStream fos = null;
            try {
                fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            parent.setDrawingCacheEnabled(true);
            Bitmap  bitmap = Bitmap.createBitmap( parent.getWidth(), parent.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            parent.draw(canvas); 
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
            return true;

        case R.id.blue:
            signature.setColor(Color.BLUE);
            return true;
        case R.id.yellow:
            signature.setColor(Color.YELLOW);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
        super.onBackPressed();
    }
}
4

2 回答 2

1
public void saveImage(Bitmap b,int count) throws Exception
    {

        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;

        String s=at.getText().toString();
        String alphaAndDigits = s.replaceAll("[^a-zA-Z0-9]+","_");


        String fileName = alphaAndDigits+"_"+wr.name+"_"+count;
        File file = new File(path, fileName+".jpg");
        newFile=Uri.fromFile(file);
        uris.add(newFile);
        fOut = new FileOutputStream(file);

        b.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();

        MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    }

试试这个功能,它对我来说效果很好,这是为了保存位图,请确保绘图缓存对你来说工作正常。

于 2013-02-21T09:24:01.503 回答
0

试试下面的代码,它可以工作

在清单中添加以下权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{
private DrawableView drawableView;   //here I have taken customview, it is optional, I am saving this view as image. You can take any layout or view
private Button saveBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    drawableView = (DrawableView) findViewById(R.id.drawable_view);
    saveBtn = (Button) findViewById(R.id.save_btn);
    saveBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
    case R.id.save_btn:
        File file = saveBitMap(this, drawableView);    //which view you want to pass that view as parameter
        if (file != null) {
            Log.i("TAG", "Drawing saved to the gallery!");
        } else {
            Log.i("TAG", "Oops! Image could not be saved.");
        }
        break;
    default:
        break;
    }

private File saveBitMap(Context context, View drawView){
    File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Handcare");
    if (!pictureFileDir.exists()) {
        boolean isDirectoryCreated = pictureFileDir.mkdirs();
        if(!isDirectoryCreated)
            Log.i("ATG", "Can't create directory to save the image");
        return null;
    }
    String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg";
    File pictureFile = new File(filename);
    Bitmap bitmap =getBitmapFromView(drawView);
    try {
        pictureFile.createNewFile();
        FileOutputStream oStream = new FileOutputStream(pictureFile);
        bitmap.compress(CompressFormat.PNG, 100, oStream);
        oStream.flush();
        oStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("TAG", "There was an issue saving the image.");
    }       
        scanGallery(pictureFile.getAbsolutePath(), context);
    return pictureFile;
}
//create bitmap from view and returns it
private Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) {
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    }   else{
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    }
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}
// used for scanning gallery
private void scanGallery(Context cntx, String path) {
    try {
        MediaScannerConnection.scanFile(cntx, new String[] { path },null, new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

于 2015-01-21T12:37:47.110 回答