Hugo AE Hugo AE - 5 months ago 52
Android Question

share the current image viewflipper

I want to share the current image being viewed from the viewflipper, but I can not get the name of the image being displayed, this is the code I use:

public class imagen1 extends Activity {
public float init_x;
private ViewFlipper vf;

int gallery_grid_Images[] = {R.drawable.fondo, R.drawable.fondo2, R.drawable.fondo3,
R.drawable.fondo4, R.drawable.fondo5
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imagen1);
ImageView imagePreview = (ImageView) findViewById(R.id.preview);

vf = (ViewFlipper) findViewById(R.id.viewFlipper);
for (int i = 0; i < gallery_grid_Images.length; i++) {
// This will create dynamic image view and add them to ViewFlipper
setFlipperImage(gallery_grid_Images[i]);
}
vf.setOnTouchListener(new ListenerTouchViewFlipper());

}

private void setFlipperImage(int res) {
Log.i("Set Filpper Called", res + "");
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(res);
vf.addView(image);
}

public void compartir (View v) {

Uri newUri2 = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + gallery_grid_Images);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "");
shareIntent.putExtra(Intent.EXTRA_STREAM, newUri2);
shareIntent.setType("image/jpg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Compartir"));

}
}


If someone could help me a little would greatly appreciate it

J j J j
Answer

Probably this should do the trick for you:

public class imagen1 extends Activity {
public float init_x;
private ViewFlipper vf;

int gallery_grid_Images[] = {R.drawable.fondo, R.drawable.fondo2, R.drawable.fondo3,
        R.drawable.fondo4, R.drawable.fondo5
};

ImageView[] views = new ImageView[gallery_grid_Images.length];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imagen1);
    ImageView imagePreview = (ImageView) findViewById(R.id.preview);

    vf = (ViewFlipper) findViewById(R.id.viewFlipper);
    for (int i = 0; i < gallery_grid_Images.length; i++) {
        //  This will create dynamic image view and add them to ViewFlipper
        setFlipperImage(gallery_grid_Images[i], i);
    }
    vf.setOnTouchListener(new ListenerTouchViewFlipper());

}

private void setFlipperImage(int res, int index) {
    Log.i("Set Filpper Called", res + "");
    ImageView image = new ImageView(getApplicationContext());
    image.setBackgroundResource(res);
    vf.addView(image);
    views[index] = image;
}

public void compartir (View v) {
    int index = -1;
    for (int i = 0; i < views.length; i++) {
        if (views[i] == (ImageView) vf.getCurrentView())
            index = i;
    }

    if (index == -1) {
        // failed to determine the right index
        Log.w("imagen1", "Could not determine the right index!");
        return; 
    }

    Uri newUri2 = Uri.parse("android.resource://" + getPackageName()
            + "/drawable/" + gallery_grid_Images[index]);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "");
    shareIntent.putExtra(Intent.EXTRA_STREAM, newUri2);
    shareIntent.setType("image/jpg");
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(shareIntent, "Compartir"));

}

}