捕获照片并将其存储在RecyclerView中

时间:2019-07-12 13:41:25

标签: java android

我想用相机拍摄一张或多张照片,并将它们存储在水平的RecyclerView中。

下面的代码给我这个错误

“原因:java.lang.NullPointerException:尝试在com.mohamedwessam.camera.MainActivity.onActivityResult(MainActivity.java:55)上写入空数组”

MainActivity

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap[] mImgUrls;
private Bitmap imageBitmap;
private Button btn_take_photo;

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

    btn_take_photo = findViewById(R.id.btn_take_photo);

    btn_take_photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dispatchTakePictureIntent();
        }
    });
}

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        imageBitmap = (Bitmap) extras.get("data");

        for (int i = 0; i <= 10; i++) {
            mImgUrls[i] = imageBitmap;
        }

        initRecyclerView();

    }
}

private void initRecyclerView() {
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(linearLayoutManager);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(mImgUrls, this);
    recyclerView.setAdapter(adapter);
}
}

RecyclerViewAdapter

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

private Bitmap[] mImgUrl;
private Context mContext;

public RecyclerViewAdapter(Bitmap[] imgUrl, Context context) {
    this.mImgUrl = imgUrl;
    this.mContext = context;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
    viewHolder.listItemImageView.setImageBitmap(mImgUrl[i]);

    viewHolder.listItemImageView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(mContext, mImgUrl.length, Toast.LENGTH_LONG).show();
            return false;
        }
    });
}

@Override
public int getItemCount() {
    return mImgUrl.length;
}


public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView listItemImageView;

    public ViewHolder(View itemView) {
        super(itemView);
        listItemImageView = itemView.findViewById(R.id.list_item_imgView);
    }
}
}

activity_main.xml

<android.support.constraint.ConstraintLayout
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:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/btn_take_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="52dp"
    android:layout_marginEnd="8dp"
    android:text="Take photo"
    android:textAllCaps="false"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recycler_view" />

list_item.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_imgView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="12dp" />

0 个答案:

没有答案