在Viewpage/Tablayout上显示在ImageView上的相机单击插入SQL中的文本信息,我如何可以检索Ima



我想从这里传递文本信息到tab1或2或3 =====> Tablayout/ViewPager我希望每个标签都有自己的数据示例:为tab1标识1个数据Id 2 tab2数据.......

this my sqlite class:

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);
    db.insert(TABLE_NAME, null, contentValues);
}

}

Tab1.class

public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab1, container, false);
    ImageView imageView = (ImageView) v.findViewById(R.id.img1);
    String photoPath = Environment.getExternalStorageDirectory() + "/Download/image1.jpg";
    Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
    imageView.setImageBitmap(b);
    return v;
}

Information.class

public class Information extends AppCompatActivity {
EditText text1,text2 ;
Button btn;
DataBaseHelper dataBaseHelper;
@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();
}

我的tablayout/viewpager类包含tab1-2-3;

 @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";
                            //  imageName(pictureName);
                            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());
}

我不明白你为什么要将sqlite数据从活动转移到片段,而你可以在片段中从sqlite数据库获取数据。但是如果你想从activity中传输数据,那么使用parcelable类,并使用bundle发送该类的对象,在fragment中获取该对象的值。

最新更新