将GridView Matter转换为.jpeg等图像文件

时间:2015-01-11 15:18:50

标签: android listview gridview

我不是很长时间在Android上命令。 但是从我学到很多的帮助。 Thx到StackOverflow社区。 现在我质疑我的努力! 请帮我。 如上所述,我想从GridView创建jpeg文件。 这是我的gridview代码。

public class MainActivity extends Activity {
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GridView lv1 = (GridView)findViewById(R.id.lv1);
    List<String> li = new ArrayList<String>();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_list_item_1,li);
    lv1.setAdapter(dataAdapter);
    dataAdapter.setDropDownViewResource(R.layout.activity_main);
    try {
        db = SQLiteDatabase.openOrCreateDatabase("/sdcard/ctron/sales.db", null);
        Cursor cr = db.rawQuery("SELECT * FROM g001", null);
        if (cr != null) {
            if (cr.moveToFirst()) {
                do {
                    String accode = cr.getString(cr.getColumnIndex("accode"));
                    String desc = cr.getString(cr.getColumnIndex("desc"));
                    String place = cr.getString(cr.getColumnIndex("place"));
                    li.add(accode);
                    li.add(desc);
                    li.add(place);
                    lv1.setAdapter(dataAdapter);
                } while (cr.moveToNext());
            } else {
                Toast.makeText(getBaseContext(), "Problem in Database!!", Toast.LENGTH_LONG)
                        .show();
            }
        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Error" + e.getMessage(),
                Toast.LENGTH_LONG).show();
}}

}

请帮我讲解如何创建图片

1 个答案:

答案 0 :(得分:0)

此代码将创建Bitmap给定View

public static Bitmap getBitmapFromView(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable!=null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);

    return bitmap;
}

要将其保存到jpeg文件,请使用Bitmap.compress

bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);