0

I want to add the images stored in the database in the image view of the ViewFlipper. But when I add those images i get some errors. Please help me out with the problem or let me know if there's any other solutions to do so. Here are my codes:

MainActivity.java

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editImagename;
Button btnAddData;
Button btnview;
ImageView imgview;
ViewFlipper viewflips;
private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    editImagename = findViewById(R.id.image_name);
    btnAddData = findViewById(R.id.addbtn);
    btnview = findViewById(R.id.getbtn);
    imgview = new ImageView(this);
    viewflips = findViewById(R.id.viewflip);
    viewAll();

    TextView txt = findViewById(R.id.text);
    txt.setSelected(true);

    Button buttonLoadImage = findViewById(R.id.btn1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(Intent.ACTION_PICK,Uri.parse("content://media/internal/images/media"));
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

    myDb = new DatabaseHelper(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        final String x = getPath(uri);
        final String ImageName = editImagename.getText().toString();

        btnAddData.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        boolean isInserted = myDb.insertData(ImageName, x);
                        if(isInserted == true)
                            Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                    }
                }
        );

    }
}

private String getPath(Uri uri) {
    if (uri==null) return null;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri,projection,null,null,null);
    if (cursor!=null){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
}

public void viewAll() {
    btnview.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    while(res.moveToNext()){
                        Uri mUri = Uri.parse(res.getString(2));
                        imgview.setImageURI(mUri);
                        viewflips.addView(imgview);
                    }
                }
            }
    );
}

}

DatabaseHelper.java

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editImagename;
Button btnAddData;
Button btnview;
ImageView imgview;
ViewFlipper viewflips;
private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    editImagename = findViewById(R.id.image_name);
    btnAddData = findViewById(R.id.addbtn);
    btnview = findViewById(R.id.getbtn);
    imgview = new ImageView(this);
    viewflips = findViewById(R.id.viewflip);
    viewAll();

    TextView txt = findViewById(R.id.text);
    txt.setSelected(true);

    VideoView videoView = findViewById(R.id.videoView);
    String videoPath = "android.resource://" + getPackageName() + "/raw/yeti";
    Uri uri = Uri.parse(videoPath);
    videoView.setVideoURI(uri);
    videoView.start();
    videoView.requestFocus();
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setLooping(true);
        }
    });

    Button buttonLoadImage = findViewById(R.id.btn1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(Intent.ACTION_PICK,Uri.parse("content://media/internal/images/media"));
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

    myDb = new DatabaseHelper(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        final String x = getPath(uri);
        final String ImageName = editImagename.getText().toString();

        btnAddData.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View v){
                        boolean isInserted = myDb.insertData(ImageName, x);
                        if(isInserted == true)
                            Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                    }
                }
        );

    }
}

private String getPath(Uri uri) {
    if (uri==null) return null;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri,projection,null,null,null);
    if (cursor!=null){
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
}

public void viewAll() {
    btnview.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor res = myDb.getAllData();
                    while(res.moveToNext()){
                        Uri mUri = Uri.parse(res.getString(2));
                        imgview.setImageURI(mUri);
                        viewflips.addView(imgview);
                    }
                }
            }
    );
}

}

And the error I get after running these codes.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 15261
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
    at android.view.ViewGroup.addView(ViewGroup.java:4768)
    at android.widget.ViewAnimator.addView(ViewAnimator.java:183)
    at android.view.ViewGroup.addView(ViewGroup.java:4708)
    at android.view.ViewGroup.addView(ViewGroup.java:4681)
    at com.example.test.MainActivity$4.onClick(MainActivity.java:118)
    at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6520)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
SUGAN
  • 3
  • 3

2 Answers2

0

Just add the viewflips.removeView() before add a view in viewflips.addView(imgview).

Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
  • It surely helped me to remove the error but all the images in database are not being displayed in the viewflipper. Please help me out with this. Only one image from the database is being displayed in the viewflipper. I want all the images from the database to be displayed in the viewflipper. Help me find out where i have done the mistakes. Thankyou for the support! – SUGAN Sep 16 '19 at 08:06
0

This error comes when ViewFlipper already have view and, ty to add more views over previous added views. So, always before you add a view, always you have to clear/remove all previous view from ViewFlipper. So, Add

viewflips.removeViews();

just before

viewflips.addView(imgview);

in everywhere.

Waseem
  • 439
  • 3
  • 18
  • 1
    It surely helped me to remove the error but all the images in database are not being displayed in the viewflipper. Please help me out with this. Only one image from the database is being displayed in the viewflipper. I want all the images from the database to be displayed in the viewflipper. Help me find out where i have done the mistakes. Thankyou for the support! – SUGAN Sep 16 '19 at 08:06