ste9206 ste9206 - 6 months ago 192
Android Question

How to return the startActivityonResult in RecyclerViewAdapter?

I've used this function to open gallery and select photo from gallery (this function is in adapter):

private class ViewSimple extends RecyclerView.ViewHolder
{
ImageView button;
public ViewSimple(View itemView)
{
super(itemView);
button = (ImageView)itemView.findViewById(R.id.imgAdding);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
FragmentActivity activity = (FragmentActivity)context;
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
activity.startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_PICTURE);

}
});

}
}


now I made this in the Fragment:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BitmapFactory.Options btmapOptions;

if (requestCode == SELECT_PICTURE && resultCode == Activity.RESULT_OK) {
final Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, this.getActivity());
final Bitmap bm;
btmapOptions = new BitmapFactory.Options();
btmapOptions.inSampleSize = 2;
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
AircraftAdapter.bmp = bm;
}
}



public String getPath(Uri uri, Activity activity)
{
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}


now I want to return the result to the cardview..how could I do it? If I return the image to server I use Picasso...it's possible to use it in this case?

EDIT: this is the fragment class:

public class AddAircraftFragment extends Fragment
{
RecyclerView recyclerView;
private static final int SELECT_PICTURE = 111;


public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{

View rootView = inflater.inflate(R.layout.fragment_add_aircraft, container, false);

recyclerView=(RecyclerView)rootView.findViewById(R.id.recyclerAircraftAdd);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
AircraftAdapter adapter = new AircraftAdapter();
recyclerView.setAdapter(adapter);


return rootView;

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BitmapFactory.Options btmapOptions;

if (requestCode == SELECT_PICTURE && resultCode == Activity.RESULT_OK) {
final Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, this.getActivity());
final Bitmap bm;
btmapOptions = new BitmapFactory.Options();
btmapOptions.inSampleSize = 2;
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
AircraftAdapter.bmp = bm;
}
}



public String getPath(Uri uri, Activity activity)
{
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}


public void onAttach(Activity activity)
{
super.onAttach(activity);
}


public void onDetach()
{
super.onDetach();
}


}

Thank you

Answer

Fragment class :

public class AddAircraftFragment extends Fragment {
RecyclerView recyclerView;
private static final int SELECT_PICTURE = 111;
private ArrayList<String> arrayList = new ArrayList<>();
private AircraftAdapter adapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_add_aircraft, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerAircraftAdd);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);
    adapter = new AircraftAdapter(getActivity(), arrayList);
    recyclerView.setAdapter(adapter);


    return rootView;

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    BitmapFactory.Options btmapOptions;

    if (requestCode == SELECT_PICTURE && resultCode == Activity.RESULT_OK) {
        final Uri selectedImageUri = data.getData();
        String tempPath = getPath(selectedImageUri, this.getActivity());
        final Bitmap bm;
        btmapOptions = new BitmapFactory.Options();
        btmapOptions.inSampleSize = 2;
        bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
        arrayList.add(tempPath);
        adapter.notifyDataSetChanged();
    }
}


public String getPath(Uri uri, Activity activity) {
    String[] projection = {MediaStore.MediaColumns.DATA};
    Cursor cursor = activity.managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}


public void onAttach(Activity activity) {
    super.onAttach(activity);
}


public void onDetach() {
    super.onDetach();
}

}

Adapter :

public class AircraftAdapter extends RecyclerView.Adapter<AircraftAdapter.ViewHolder> {

private Context mContext;
private ArrayList<String> arrayList;

public AircraftAdapter(
        Context context,
        ArrayList<String> aryLstMyCarModel) {

    mContext = context;
    this.arrayList = aryLstMyCarModel;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.your_xml, parent, false);
    return new ViewHolder(v);
}


@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    if (position == getItemCount()) {
    }
    if (arrayList.size() != 0) {

        File f = new File(arrayList.get(position));
        Bitmap bitmap = decodeFile(f, your_width,
                your_height);
        holder.imageview.setImageBitmap(bitmap);
    }
}
@Override
public int getItemCount() {
    return arrayList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imageview;
    public ViewHolder(View itemView) {
        super(itemView);

        imageview=(RelativeLayout)itemView.findViewById(R.id.imageview);
    }
   }
}

For decode

// to scale the bitmap
private Bitmap decodeFile(File f, int WIDTH, int HIGHT) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_WIDTH = WIDTH;
        final int REQUIRED_HIGHT = HIGHT;
        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}