How to Pass the Image Name Dynamically?

0

Hello, I'm learning now and I was able to make a Successful Crown or Application application and I decided to create another one based on it. This other application appears from a click of the mainActivity button it calls a second activity that randomly displays an image of the 36 images that are in the drawable folder. I would like to share and here I even found the answer to do this tasks, but when trying to create a method to share, I came across the following difficulty: I do not know how to pass the image name dynamically so I can reuse the method for all 36 without having that rewrite 36 times, I imagine it is possible but I have not figured out how to treat the information correctly. Here is the snippet of the code I am referring to:

I call the method checkPermission here:

if( extra != null){

if (opcaoEscolhida.equals("s1")){
    imagem.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.s1));
    imagem.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View v) {
                                      checarPermissao("s1");
                                  }
                              }
    );

The methods I found here and adapted:

private void checarPermissao(String img){
    // Verifica  o estado da permissão de WRITE_EXTERNAL_STORAGE
    int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        // Se for diferente de PERMISSION_GRANTED, então vamos exibir a tela padrão
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, SOLICITAR_PERMISSAO);
    } else {
        // Senão vamos compartilhar a imagem
        sharedImage(img);
    }
}

private void sharedImage(String img){
    // Vamos carregar a imagem em um bitmap

    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.s1);  //aqui eu queria que recebe cada "sN" das imagens... mas nao sei como fazer...
    Intent share = new Intent(Intent.ACTION_SEND);
    //setamos o tipo da imagem
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    // comprimomos a imagem
    b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    // Gravamos a imagem
    String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Sorte de Hoje", null);
    // criamos uam Uri com o endereço que a imagem foi salva
    Uri imageUri =  Uri.parse(path);
    // Setmaos a Uri da imagem
    share.putExtra(Intent.EXTRA_STREAM, imageUri);
    // chama o compartilhamento
    startActivity(Intent.createChooser(share, "Compartilhe"));
}

The solution that worked for the end was the below, unfortunately repeating the code, because I could not create a method passing the information and made this gambiarra, argh ...

public class SorteActivity extends AppCompatActivity {

private ImageView imagem;
//private ImageView botaoVoltar;
private static final int SOLICITAR_PERMISSAO = 1;

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

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    imagem = (ImageView) findViewById(R.id.sorteId);
    //botaoVoltar = (ImageView) findViewById(R.id.botaoVoltarId);

    Bundle extra = getIntent().getExtras();
    String opcaoEscolhida = extra.getString("opcao");

    if( extra != null){

        if (opcaoEscolhida.equals("s1")){
            imagem.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.s1));
            imagem.setOnClickListener(new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {
                                              //checarPermissao();
                                              int permissionCheck = ContextCompat.checkSelfPermission(SorteActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
                                              if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                                                  ActivityCompat.requestPermissions(SorteActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, SOLICITAR_PERMISSAO);
                                              } else {
                                                  Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.s1);
                                                  Intent share = new Intent(Intent.ACTION_SEND);
                                                  //setamos o tipo da imagem
                                                  share.setType("image/jpeg");
                                                  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                                                  // comprimomos a imagem
                                                  b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                                                  // Gravamos a imagem
                                                  String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Sorte de Hoje", null);
                                                  // criamos uam Uri com o endereço que a imagem foi salva
                                                  Uri imageUri = Uri.parse(path);
                                                  // Setmaos a Uri da imagem
                                                  share.putExtra(Intent.EXTRA_STREAM, imageUri);
                                                  // chama o compartilhamento
                                                  startActivity(Intent.createChooser(share, "Compartilhe"));
                                              }
                                          }
                                      }
            );
        }else if (opcaoEscolhida.equals("s2")) { ... e assim por diante até o s35... #cruzcredo...
    
asked by anonymous 30.05.2017 / 02:44

1 answer

0

You can make a list of all your images like this:

list.add(R.drawable.s1);
list.add(R.drawable.s2);
list.add(R.drawable.s3);
list.add(R.drawable.s4);

To get the R.drawable.sN just make a for that this is about the problem!

for(int i =1; i<36; i++){
     list.get(i);
}

Some doubts or some error says :) I hope this helps !!

    
31.05.2017 / 11:20