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:
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);
}
}
}
);
}
}
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)
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.