如何在适配器中实现onActivityResult?

问题描述 投票:0回答:2

有人可以用一些代码片段向我解释一下我们如何在适配器内恢复onActivityResult的结果?我已经阅读了一些有关接口的内容,但我无法尝试实现这一点。谢谢!

示例:在适配器内部我有一个ViewHolder,其中是一个ImageView.An事件,单击此ImageView启动startActivityForResult.But在同一个适配器中我需要通过onActivityResult方法恢复结果。

如何处理以下内容:

主要内容:

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<Filme> listaFilmes = new ArrayList<>();

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

        recyclerView = findViewById(R.id.recyclerView);

        //Listagem de filmes
        this.criarFilmes();

        //Configurar adapter
        final Adapter adapter = new Adapter(listaFilmes,getApplicationContext(),MainActivity.this);


        //Configurar Recyclerview
        //Para mostrar na horizontal: new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration( new DividerItemDecoration(this, LinearLayout.VERTICAL));
        recyclerView.setAdapter(adapter);

    }

    public void criarFilmes(){
        Filme filme = new Filme("Homem Aranha - De volta ao lar","Aventura","2017");
        this.listaFilmes.add( filme );

    }
}

适配器:

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    private List<Filme> listaFilmes;
    private Context context;
    private Activity activity;
    private ImageView imagem;
    private Uri photoURI;
    Uri filePath;
    String mCurrentPhotoPath;
    static final int REQUEST_TAKE_PHOTO = 1;



    public Adapter(List<Filme> listaFilmes, Context context,Activity activity) {
        this.listaFilmes = listaFilmes;
        this.context = context;
        this.activity = activity;
    }

    //Aqui ele cria apenas a visualização
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemLista = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_lista, parent, false);

        return new MyViewHolder(itemLista);
    }

    //A exibição dos itens ocorre aqui
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        final Filme filme = listaFilmes.get(position);
        /*holder.titulo.setText(filme.getTituloFilme());
        holder.genero.setText(filme.getGenero());
        holder.ano.setText(filme.getAno());*/
        holder.imagem.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(context, "imagem removida", Toast.LENGTH_SHORT).show();
                listaFilmes.remove(position);
                notifyDataSetChanged();
                return false;
            }
        });
        holder.imagem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "imagem clicada", Toast.LENGTH_SHORT).show();
                listaFilmes.add(position+1,filme);
                notifyDataSetChanged();
                selectImage();


            }
        });
    }

    //Retorna a quantidade de itens que serão exibidos
    @Override
    public int getItemCount() {
        return listaFilmes.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        /*TextView titulo;
        TextView ano;
        TextView genero;*/
        ImageView imagem;

        public MyViewHolder(View itemView) {
            super(itemView);

            imagem = itemView.findViewById(R.id.image_id);
            /*titulo = itemView.findViewById(R.id.textTitulo);
            ano    = itemView.findViewById(R.id.textAno);
            genero = itemView.findViewById(R.id.textGenero);*/
        }
    }

    /*Função seleciona foto da galeria ou tira foto*/
    public void selectImage() {

        final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo"))
                {
                    dispatchTakePictureIntent(activity);
                }
                else if (options[item].equals("Choose from Gallery"))
                {
                    Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    ((Activity) context).startActivityForResult(intent, 2);
                }
                else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    //ativa a camera para tirar foto
    public void dispatchTakePictureIntent(Activity currentActivity) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Ensure that there`s a camera activity to handle the intent
        if(takePictureIntent.resolveActivity( context.getApplicationContext().getPackageManager() ) != null ){ //getActivity
            //Create the File where the photo should go
            File photoFile = null;
            try{
                photoFile = createImageFile(currentActivity);
            }catch(IOException ex){
                //Error occurred while creating the File
                ex.printStackTrace();

            }
            //Continue only if the File was successfully created
            if(photoFile != null){
                Log.i("ENTFR11","photofile:"+ photoFile); //    /storage/emulated/0/Android/data/com.droidelivery.droidelivery/files/Pictures/JPEG_20171010_205010_1588304974.jpg
                photoURI = FileProvider.getUriForFile(context.getApplicationContext(), //getActivity
                        "com.example.lucas.recyclerview.fileprovider",
                        photoFile);
                Log.i("ENTFR12","photofURI:"+ photoURI); //    content://com.example.android.fileprovider/my_images/JPEG_20171010_205010_1588304974.jpg
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); //ESTUDAR ESSA LINHA ESTÄ COM PROBLEMA.COMO RECUPERAR EXTRA_OUTPUT
                Log.i("ENTFR13","photoURI: " + photoURI);
                ((Activity) context).startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

    //Create a collision-resistant file name
    public File createImageFile(Activity currentActivity) throws IOException {
        //Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault() ).format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = currentActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName, /*prefix*/
                ".jpg",        /*suffix*/
                storageDir     /*directory*/
        );

        //Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

    public void setPic() {
        // Get the dimensions of the View

        int targetW = imagem.getWidth();
        int targetH = imagem.getHeight();

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        imagem.setImageBitmap(bitmap);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK ) { //lembrar que activity que requisita foto nao retorna necessariamente uma URI!!!
            //filePath = data.getData();
            //filePath = photoURI;
            setPic();
        }
        if(requestCode == 2 && resultCode == RESULT_OK && data != null){
            Toast.makeText(context, "requestCode == 2", Toast.LENGTH_SHORT).show();
            try {
                //getting image from gallery
                Uri selectedImage = data.getData();
                Log.i("SSS","selectedImage: " + selectedImage);
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), selectedImage);//getContext

                imagem.setImageBitmap(bitmap);
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
}
android android-adapter android-viewholder onactivityresult startactivityforresult
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.