如何从另一个 activity 中的 uri 设置导航抽屉查看器的图像

How to set image of navigation drawer viewer from uri in another activity

如何设置在带有下载 URI 的导航抽屉中显示的用户图像

public class UserProfileActivity extends AppCompatActivity {

    private FloatingActionButton buttonSaveImage;
    private FloatingActionButton buttonSelectImage;
    private ImageView imageView;
    private static final int PICK_IMAGE_REQUEST = 123;
    private Uri filePath;
    private FirebaseAuth mAuth;
    private FirebaseStorage fStorage;
    private ProgressDialog progressDialog;

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

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                Picasso.with(UserProfileActivity.this).load(filePath).fit().centerCrop().into(imageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


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

        mAuth = FirebaseAuth.getInstance();
        fStorage = FirebaseStorage.getInstance();


        buttonSelectImage = (FloatingActionButton) findViewById(R.id.fab);
        buttonSaveImage = (FloatingActionButton) findViewById(R.id.fab2);
        imageView = (ImageView) findViewById(R.id.imageView);


        progressDialog = new ProgressDialog(UserProfileActivity.this);
        progressDialog.setMessage("Yükleniyor...");
        progressDialog.setCancelable(false);
        progressDialog.show();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        final View headerView = navigationView.getHeaderView(0);

        final ImageView imageViewUser =  (ImageView)headerView.findViewById(R.id.profile_image);




        StorageReference storageRef = fStorage.getReference().child("users").child(mAuth.getCurrentUser().getUid());
        storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {

                Log.i("URI VALUE","->"+ uri);
                final ImageView imageViewUser =  (ImageView)headerView.findViewById(R.id.profile_image);
                progressDialog.dismiss();
                Picasso.with(UserProfileActivity.this).load(uri).fit().centerCrop().into(imageView);
                Picasso.with(UserProfileActivity.this).load(uri).fit().centerCrop().into(imageViewUser);

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                progressDialog.dismiss();
                Toast.makeText(UserProfileActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });


        buttonSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonSelectImage.setVisibility(View.INVISIBLE);

                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Resim Seçiniz"), PICK_IMAGE_REQUEST);

                buttonSaveImage.setVisibility(View.VISIBLE);
            }
        });

        buttonSaveImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                buttonSaveImage.setVisibility(View.INVISIBLE);
                if (filePath != null) {

                    progressDialog = new ProgressDialog(UserProfileActivity.this);
                    progressDialog.setMessage("Yükleniyor...");
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                    StorageReference storageRef = fStorage.getReference().child("users").child(mAuth.getCurrentUser().getUid());
                    storageRef.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            progressDialog.dismiss();
                            Toast.makeText(UserProfileActivity.this, "Fotoğraf başarılı bir şekilde kaydedildi.", Toast.LENGTH_SHORT).show();
                            imageView.setImageBitmap(null);

                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                            progressDialog.dismiss();
                            Toast.makeText(UserProfileActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();

                        }
                    });


                }
                buttonSelectImage.setVisibility(View.VISIBLE);

            }
        });
    }
}

我使用 firebase 存储来存储图像并且我做得很好,但是我无法在导航抽屉图像查看器中显示我为用户上传的图像

导航header

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@color/overloyBackground"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp">

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/user"
        app:border_color="@android:color/white"
        app:border_width="3dp" />

    <TextView
        android:id="@+id/txtFullName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:textSize="24sp"
        android:text=""
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>

activity_user_profile

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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/viewA"
        android:layout_width="match_parent"
        android:layout_height="463dp"
        android:layout_weight="0.6"
        android:background="@android:color/white"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/no_image_user" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/viewB"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="53dp"
            android:layout_weight="1"
            android:text="Name"
            android:textSize="25sp"
            />
    </LinearLayout>

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="69dp"
    android:layout_height="69dp"
    android:layout_margin="16dp"
    android:background="@android:color/transparent"
    android:clickable="true"
    android:src="@drawable/camera"
    app:layout_anchor="@id/viewA"
    app:layout_anchorGravity="bottom|right|end"
    tools:layout_editor_absoluteX="250dp"
    tools:layout_editor_absoluteY="146dp" />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="69dp"
    android:layout_height="69dp"
    android:layout_margin="16dp"
    android:background="@android:color/transparent"
    android:clickable="true"
    android:visibility="visible"
    android:src="@drawable/onay"
    app:layout_anchor="@id/viewA"
    app:layout_anchorGravity="bottom|right|end"
    tools:layout_editor_absoluteX="299dp"
    tools:layout_editor_absoluteY="146dp"/>

这是我的logcat

FATAL EXCEPTION: main Process: com.example.ytam.fonetegitim, PID: 9482 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ytam.fonetegitim/com.example.ytam.fonetegitim.UserProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' on a null object reference at com.example.ytam.fonetegitim.UserProfileActivity.onCreate(UserProfileActivity.java:77) at android.app.Activity.performCreate(Activity.java:6999) at android.app.Activity.performCreate(Activity.java:6990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

试试下面的代码

  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);


StorageReference storageRef = fStorage.getReference().child("users").child(mAuth.getCurrentUser().getUid());
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        View headerView = navigationView.getHeaderView(0);

final ImageView imageViewUser =  (ImageView)headerView.findViewById(R.id.profile_image);
        progressDialog.dismiss();

        Picasso.with(UserProfileActivity.this).load(uri).fit().centerCrop().into(imageViewUser);

    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {

        progressDialog.dismiss();
        Toast.makeText(UserProfileActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();

    }
});

更改此您的 ID 不匹配

使用这个

final ImageView imageViewUser =  (ImageView)headerView.findViewById(R.id.profile_image);

而不是这个

final ImageView imageViewUser =  (ImageView)headerView.findViewById(R.id.txtFullName);

编辑

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    View headerView = navigationView.getHeaderView(0);

    final ImageView imageViewUser =  (ImageView)headerView.findViewById(R.id.profile_image);



    StorageReference storageRef = fStorage.getReference().child("users").child(mAuth.getCurrentUser().getUid());
    storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {

            Log.i("URI VALUE","->"+ uri)
            progressDialog.dismiss();

            Picasso.with(UserProfileActivity.this).load(uri).fit().centerCrop().into(imageViewUser);

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
 cannot be converted to OnNavigationItemSelectedListener get this error on the line View headerView = navigationView.getHeaderView(0);
            progressDialog.dismiss();
            Toast.makeText(UserProfileActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

编辑新

final ImageView imageViewUser =  (ImageView)navigationView.getHeaderView(0).(R.id.profile_image);

通过这种方法,我将 url 保存到我的数据库存储

    private void changeUserPhoto() {

    StorageReference storageRef = fStorage.getReference().child("users").child(mAuth.getCurrentUser().getUid());
    storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {

            photoURL = uri.toString();
            Picasso.with(UserProfileActivity.this).load(uri).fit().centerCrop().into(imageView);

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {


        }
    });

}

此外,我使用 url 制作导航 header 图片

private void changePhoto(String myUrl) {

    Picasso.with(this).load(myUrl).transform(new PhotoCircleTransform()).into(profile_image);
}