如何使用下载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存储来存储图像,我做得很好,但是我无法在导航抽屉图像查看器中显示我为用户上传的图像
Navigaiton标题
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
致命的例外:主要 处理:com.example.ytam.fonetegitim,PID:9482 java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.ytam.fonetegitim / com.example.ytam.fonetegitim.UserProfileActivity}: java.lang.NullPointerException:尝试调用虚方法 “android.view.View android.support.design.widget.NavigationView.getHeaderView(int)'上的一个 null对象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 在android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)'上的一个 null对象引用 在 com.example.ytam.fonetegitim.UserProfileActivity.onCreate(UserProfileActivity.java:77) 在android.app.Activity.performCreate(Activity.java:6999) 在android.app.Activity.performCreate(Activity.java:6990) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 在android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:1)
更改此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);
答案 1 :(得分:0)
尝试以下代码
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();
}
});
答案 2 :(得分:0)
使用此方法,我将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制作导航标题图像
private void changePhoto(String myUrl) {
Picasso.with(this).load(myUrl).transform(new PhotoCircleTransform()).into(profile_image);
}