将图像保存在SQLite数据库中,并在应用重新打开时显示它们

时间:2016-09-07 07:32:59

标签: android imageview android-sqlite

我是Android的新手,所以请帮我完成任务。

问题:如何在SQLite中放置图片路径,当我重新启动应用时,会显示我从相机中拍摄的照片?

我想用文本将照片保存在数据库中,然后在重新启动应用程序时恢复图像视图中的图片,当我点击图像时,它会显示我在其上写的信息。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout1);

    tabLayout = (TabLayout) findViewById(R.id.tablayout1);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab3"));

    tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) findViewById(R.id.viewpager1);
    Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setAdapter(adapter);
    tabLayout.setOnTabSelectedListener(this);
    viewPager.getCurrentItem();
    viewPager.setOffscreenPageLimit(3);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab1);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            int position = tabLayout.getSelectedTabPosition();
            switch (position) {

                case 0:
                    File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                    String pictureName = "image1" + ".jpg";
                    File imageFile = new File(file, pictureName);
                    Uri pictureUri = Uri.fromFile(imageFile);

                    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    i.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
                    startActivityForResult(i, 0);
                    break;

                case 1:
                    File file2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                    String pictureName2 = "image2" + ".jpg";
                    File imageFile2 = new File(file2, pictureName2);
                    Uri pictureUri2 = Uri.fromFile(imageFile2);
                    Intent ii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    ii.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri2);
                    startActivityForResult(ii, 1);
                    break;

                case 2:
                    File file3 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                    String pictureName3 = "image3" + ".jpg";
                    File imageFile3 = new File(file3, pictureName3);
                    Uri pictureUri3 = Uri.fromFile(imageFile3);
                    Intent iii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    iii.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri3);
                    startActivityForResult(iii, 2);
                    break;
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    imageView1 = (ImageView) findViewById(R.id.img1);
    imageView2 = (ImageView) findViewById(R.id.img2);
    imageView3 = (ImageView) findViewById(R.id.img3);

    if (requestCode == 0 && resultCode == RESULT_OK) {

        String photoPath = Environment.getExternalStorageDirectory() + "/Download/image1.jpg";
        galleryAddPic(photoPath);
        Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
        imageView1.setImageBitmap(b);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView1.buildDrawingCache();
                Bitmap image = imageView1.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);

                startActivity(o);

            }
        });

    } else if (requestCode == 1 && resultCode == RESULT_OK) {

        String photoPath = Environment.getExternalStorageDirectory() + "/Download/image2.jpg";
        galleryAddPic(photoPath);

        Bitmap bitmap2 = BitmapFactory.decodeFile(photoPath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        final Bitmap b2 = BitmapFactory.decodeFile(photoPath, options);
        imageView2.setImageBitmap(b2);
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView2.buildDrawingCache();
                Bitmap image = imageView2.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);
                startActivity(o);

            }
        });

    } else if (requestCode == 2 && resultCode == RESULT_OK) {

        String photoPath = Environment.getExternalStorageDirectory() + "/Download/image3.jpg";
        galleryAddPic(photoPath);

        Bitmap bitmap3 = BitmapFactory.decodeFile(photoPath);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        final Bitmap b3 = BitmapFactory.decodeFile(photoPath, options);
        imageView3.setImageBitmap(b3);
        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView3.buildDrawingCache();
                Bitmap image = imageView3.getDrawingCache();

                Bundle extras = new Bundle();
                Intent o = new Intent(Layout1.this, Information.class);
                extras.putParcelable("Bitmap", image);
                o.putExtras(extras);
                startActivity(o);
            }
        });
    }
}
private void galleryAddPic(String photoPath) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(photoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

这是我的数据库类:

public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "big.db";
    public static final String TABLE_NAME = "images";
    public static final String NAME = "name";
    public static final String PLACE = "place";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_IMAGES_TABLE = "CREATE TABLE images ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +

            "name TEXT, " +
            "place TEXT )";

    db.execSQL(CREATE_IMAGES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS images");
    this.onCreate(db);
}

public void insertentry(String name, String place) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME, name);
    contentValues.put(PLACE, place);
    contentValues.put(PLACE, place);

    db.insert(TABLE_NAME, null, contentValues);
 }}

这是从我的相机拍摄照片并将信息放入其中的信息类

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_information);

    text1 = (EditText) findViewById(R.id.txt1);
    text2 = (EditText) findViewById(R.id.txt2);
    ImageView oo= (ImageView)findViewById(R.id.imageView99);

    btn = (Button) findViewById(R.id.btn1);
    Log.d("CLick", "123 INFO SAVED");
    addData();

    dataBaseHelper = new DataBaseHelper(this);

    Bundle extras = getIntent().getExtras();
    Bitmap bmp = (Bitmap) extras.getParcelable("Bitmap");
    oo.setImageBitmap(bmp);
}

public void addData(){
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dataBaseHelper.insertentry(text1.getText().toString(),text2.getText().toString());
            Toast.makeText(getApplicationContext(),"doneeeee",
                    Toast.LENGTH_LONG).show();
        }
    });

}

0 个答案:

没有答案