Android 用相机图像填充RecyclerView

Android 用相机图像填充RecyclerView,android,android-recyclerview,Android,Android Recyclerview,在从相机拍摄到一张照片后,我试着用新的行填充一个回收视图。显然,我的解决方案不起作用,但我不知道为什么。抓获后,什么也没有显示。 这是我的四个.java文件: 1.主课 2.适配器 3.持票人 4.模型 这是我认为错误所在的主类的代码: public class ComplaintActivity extends AppCompatActivity { private static final int SELECT_PICTURE = 1; private static final int C

在从相机拍摄到一张照片后,我试着用新的行填充一个回收视图。显然,我的解决方案不起作用,但我不知道为什么。抓获后,什么也没有显示。 这是我的四个.java文件: 1.主课 2.适配器 3.持票人 4.模型

这是我认为错误所在的主类的代码:

public class ComplaintActivity extends AppCompatActivity {

private static final int SELECT_PICTURE = 1;
private static final int CAMERA_REQUEST = 1888;

private Button btnGallery;
private Button btnCamera;
private ImageView imageView;

private String selectedImagePath;

private static RecyclerView recyclerView;

private static ComplaintAdapter adapter;

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

    btnCamera = (Button) this.findViewById(R.id.btnCamera);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });

    recyclerView = (RecyclerView) findViewById(R.id.recyclerViewComplaint);
    adapter = new ComplaintAdapter(this, new ArrayList<ComplaintModel>());
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        loadRecyclerView(photo);
    }
}

private void loadRecyclerView(Bitmap image){

    ComplaintModel data = new ComplaintModel();
    ArrayList<ComplaintModel> notificationsList = new ArrayList<>();

    data.setId(1);
    data.setUsername("Try");
    data.setText("Try");
    data.setDate("05-07-2017");
    data.setTime("Prova");

    notificationsList.add(data);

    adapter = new ComplaintAdapter(this, notificationsList);
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);
}

/**
 * helper to retrieve the path of an image URI
 */
public String getPath(Uri uri) {
    // just some safety built in
    if( uri == null ) {
        // TODO perform some logging or show user feedback
        return null;
    }
    // try to retrieve the image from the media store first
    // this will only work for images selected from gallery
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }
    // this is our fallback here
    return uri.getPath();
}

@Override
public void onBackPressed() {
    super.onBackPressed();

    overridePendingTransition(R.anim.left_enter, R.anim.right_out);
}
公共类投诉活动扩展了AppCompative活动{
私有静态最终整数选择_PICTURE=1;
专用静态最终int摄像机_请求=1888;
私用按钮;
私人按钮btnCamera;
私人影像视图;
私有字符串selectedImagePath;
私有静态RecyclerView RecyclerView;
专用静态适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_denuncia);
btnCamera=(按钮)this.findviewbyd(R.id.btnCamera);
btnCamera.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
}
});
recyclerView=(recyclerView)findViewById(R.id.recyclerViewComplaint);
adapter=new-completadapter(这是新的ArrayList());
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
recyclerView.setAdapter(适配器);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==CAMERA\u请求和&resultCode==Activity.RESULT\u确定){
位图照片=(位图)数据.getExtras().get(“数据”);
loadRecyclerView(照片);
}
}
私有void loadRecyclerView(位图图像){
ComplaintModel数据=新的ComplaintModel();
ArrayList notificationsList=新建ArrayList();
data.setId(1);
data.setUsername(“Try”);
data.setText(“Try”);
数据设置日期(“2017年7月5日”);
数据设置时间(“Prova”);
通知列表添加(数据);
适配器=新的投诉适配器(此,通知列表);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(适配器);
}
/**
*帮助器检索图像URI的路径
*/
公共字符串getPath(Uri){
//只是一些内置的安全装置
if(uri==null){
//TODO执行一些日志记录或显示用户反馈
返回null;
}
//首先尝试从媒体存储中检索图像
//这仅适用于从库中选择的图像
字符串[]投影={MediaStore.Images.Media.DATA};
Cursor Cursor=managedQuery(uri、投影、null、null、null);
如果(光标!=null){
int column_index=游标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
字符串路径=cursor.getString(列索引);
cursor.close();
返回路径;
}
//这是我们的退路
返回uri.getPath();
}
@凌驾
public void onBackPressed(){
super.onBackPressed();
覆盖转换(R.anim.left\u输入,R.anim.right\u输出);
}
这是XML布局(重点是按钮和RecyclerView):



谢谢您的帮助!

您的清单中是否包含相机权限?
是的,清单包含所有权限needed@BigMeister你的问题解决了吗?
<?xml version="1.0" encoding="utf-8"?>
<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">

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/White"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:padding="20dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:orientation="horizontal"
                android:paddingLeft="30dp"
                android:paddingRight="30dp">

                <Button
                    android:id="@+id/btnCamera"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_round"
                    android:text="Capture"
                    android:textColor="@color/White" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_round"
                    android:text="Gallery"
                    android:textColor="@color/White" />
            </LinearLayout>

        </LinearLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="my.axicura.app.activity.Complaint.ComplaintActivity">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerViewDenuncia"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0" />

        </android.support.constraint.ConstraintLayout>

    </LinearLayout>
</ScrollView>