如何维护在RecyclerView上从相机拍摄的照片

时间:2019-03-05 22:01:40

标签: java android

这是我第一次使用StackOverFlow和Android Java语言。

我对我的原型应用程序有疑问。

我要解释一下:

我有一个适用的适配器:

ReDatabaseAdapter

    package prototype.es.applicationdb.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import prototype.es.applicationdb.R;
import prototype.es.applicationdb.utils.ImageGetter;
import prototype.es.applicationdb.utils.ReDatabaseEntry;

public class ReDatabaseAdapter extends RecyclerView.Adapter<ReDatabaseAdapter.ReDatabaseViewHolder> {

    protected List<ReDatabaseEntry> mDB = null;

    public ReDatabaseAdapter(List<ReDatabaseEntry> list){
        mDB = new ArrayList<ReDatabaseEntry>(list);
    }

    @NonNull
    @Override
    public ReDatabaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {


        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_holder_layout,viewGroup,false);

        return new ReDatabaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReDatabaseViewHolder reDatabaseViewHolder, int position) {

        ReDatabaseEntry entry = mDB.get(position);
        String name = entry.getName();

        reDatabaseViewHolder.mName.setText(name);

        int icon_id = ImageGetter.getIcon(name);

        Context context = reDatabaseViewHolder.itemView.getContext();

        reDatabaseViewHolder.mIcon.setImageDrawable(ContextCompat.getDrawable(
          context,icon_id));

    }

    @Override
    public int getItemCount() {
    // total elements on JSON.
        return mDB.size();
    }

    public class ReDatabaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        protected TextView mName = null;
        protected ImageView mIcon = null;



        public ReDatabaseViewHolder(@NonNull View itemView) {

            super(itemView);


            mName = itemView.findViewById(R.id.tv_char_name);
            mIcon = itemView.findViewById(R.id.iv_char_icon);
            mIcon.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            int viewId=v.getId();

            if(viewId == mIcon.getId()) {

                Intent intentToTakePicture = new Intent();
                intentToTakePicture.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                MainActivity contextAux = (MainActivity) mContext;

                if (intentToTakePicture.resolveActivity(mContext.getPackageManager()) != null) {
                    contextAux.startActivityForResult(intentToTakePicture, 2);
                }
            }
        }
    }
}

在我的 MainActivity 中,我有:

    package prototype.es.applicationdb;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;
import org.json.JSONException;
import java.io.IOException;
import java.util.List;
import prototype.es.applicationdb.adapter.ReDatabaseAdapter;
import prototype.es.applicationdb.utils.ReDatabaseEntry;
import prototype.es.applicationdb.utils.ReJsonParser;


public class MainActivity extends AppCompatActivity {

    protected List<ReDatabaseEntry> mList = null;
    protected RecyclerView mRecyclerView = null;
    protected ReDatabaseAdapter mAdapter = null;
    protected LinearLayoutManager mManager = null;
    protected ImageView mPhoto;

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

        try {
            mList = ReJsonParser.parseJSONDatabase(getResources().openRawResource(R.raw.sw_db));
        } catch(IOException e){
            Log.e(getClass().getName(),e.getMessage());
        } catch(JSONException e){
            Log.e(getClass().getName(),e.getMessage());
        }

        mRecyclerView = findViewById(R.id.rv_database_viewer);

        mAdapter = new ReDatabaseAdapter(mList);

        mManager = new LinearLayoutManager(this);

        mManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.setLayoutManager(mManager);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==2 && resultCode == RESULT_OK){
            //Get results from data
            Bitmap img = (Bitmap) data.getExtras().get("data");
            Bitmap bitmap = Bitmap.createScaledBitmap(img, 600, 800, false);
            mPhoto = findViewById(R.id.iv_char_icon);
            mPhoto.setImageBitmap(bitmap);
        }
    }

}

获取图像

package prototype.es.applicationdb.utils;

import prototype.es.applicationdb.R;

public class ImageGetter {

    public static int getIcon(String name){
        switch(name){
            case "forest" :
                return R.drawable.forest;
            case "beach" :
                return R.drawable.beach;
            case "storms":
                return R.drawable.storms;
            case "design":
                return R.drawable.design;
            case "architecture" :
                return R.drawable.architecture;
            case "technologies":
                return R.drawable.technologies;
            case "music":
                return R.drawable.music;
            case "food":
                return R.drawable.food;
            case "animals":
                return R.drawable.animals;
            case "countries":
                return R.drawable.countries;
            case "transport":
                return R.drawable.transport;
            case "sports":
                return R.drawable.sports;
            case "fashion":
                return R.drawable.fashion;
            case "news":
                return R.drawable.news;
            default:
                return R.drawable.defaultIcon;
        }
    }
}

布局view_holder:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_outer_card"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card_border">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_gravity="center_horizontal"/>

...

布局activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/rv_db_viewer"/>

</LinearLayout>

问题是当我长时间滚动时,我的Recycler破坏了我的照片并显示原始图片。已为从Parse JSON调用的适配器恢复了原始图片

我如何维护我的照片? 我正在尝试创建一个列表,单击一个图标即可将其附加在该列表中并在列表中进行维护。 / strong>

我需要您的帮助,一些想法将不胜感激

谢谢!

1 个答案:

答案 0 :(得分:0)

您好,请按照本教程的步骤进行操作。 我们了解您的问题,因此,如果需要,请按照以下说明进行操作 简易的相机代码,然后click here

步骤1:创建 DemoModel.java

import android.graphics.Bitmap;
import android.net.Uri;

public class DemoModel {
private String imagename = "";
private Bitmap imageBitmap= null;

public String getImagename() {
    return imagename;
}

public void setImagename(String imagename) {
    this.imagename = imagename;
}

public Bitmap getImageBitmap() {
    return imageBitmap;
}

public void setImageBitmap(Bitmap imageBitmap) {
    this.imageBitmap = imageBitmap;
}
}

第2步:创建 row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/ivImage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher_round" />

<TextView
    android:id="@+id/tvText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical" />
</LinearLayout>

第3步:创建 DemoAdapter.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> 
{
private String TAG = "GroupAdapter";
private Context mContext;
private LayoutInflater infalter;
private ArrayList<DemoModel> mDemoModelArrayList = new ArrayList<DemoModel>();
private View.OnClickListener mItemClickListener;

public DemoAdapter(Context context, View.OnClickListener itemClickListener, ArrayList<DemoModel> demoModelArrayList) {

    this.infalter = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mContext = context;

    this.mDemoModelArrayList = demoModelArrayList;
    this.mItemClickListener = itemClickListener;

}

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

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


    holder.tvText.setText(mDemoModelArrayList.get(position).getImagename());
    holder.ivImage.setImageBitmap(mDemoModelArrayList.get(position).getImageBitmap());

    holder.ivImage.setTag(position);
    holder.ivImage.setOnClickListener(mItemClickListener);
}

@Override
public int getItemCount() {
    return mDemoModelArrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView tvText;
    private ImageView ivImage;

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

        tvText = (TextView) itemView.findViewById(R.id.tvText);
        ivImage = (ImageView)itemView.findViewById(R.id.ivImage);

    }
}
}

第4步:在我们的 MainActivity.java

中编写此代码
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private RecyclerView rvrecylerView;
private DemoAdapter mDemoAdapter;
private ArrayList<DemoModel> mDemoModelArrayList = new ArrayList<DemoModel>();
int positionGl =0;

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

    setDummyData();

    rvrecylerView = (RecyclerView)findViewById(R.id.rvrecylerView);

    LinearLayoutManager mLinearLayout = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
    mLinearLayout.setSmoothScrollbarEnabled(true);
    rvrecylerView.setLayoutManager(mLinearLayout);
    rvrecylerView.setItemAnimator(new DefaultItemAnimator());
    mDemoAdapter = new DemoAdapter(MainActivity.this,mItemClickListener,mDemoModelArrayList);
    rvrecylerView.setAdapter(mDemoAdapter);
}

private void setDummyData() {
    for (int i = 0; i < 50; i++) {
        DemoModel tempDemoModel = new DemoModel();
        tempDemoModel.setImagename("MyName "+i);
        mDemoModelArrayList.add(tempDemoModel);
    }
}
private View.OnClickListener mItemClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int position =  (int)v.getTag();
        positionGl = position;
        openCamera(position);
    }
};

private void openCamera(int postion) {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 502);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 502 && resultCode == RESULT_OK && data != null) {
        Bitmap mphoto = (Bitmap) data.getExtras().get("data");
        mDemoModelArrayList.get(positionGl).setImageBitmap(mphoto);
        mDemoAdapter.notifyDataSetChanged();
    }
}