如何在另一个活动中从 uri 设置导航抽屉查看器的图像
Posted
技术标签:
【中文标题】如何在另一个活动中从 uri 设置导航抽屉查看器的图像【英文标题】:How to set image of navigation drawer viewer from uri in another activity 【发布时间】:2018-06-09 17:14:45 【问题描述】:我如何设置导航抽屉中显示的用户图像和下载 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 存储来存储图像,我做得很好,但我无法在导航抽屉图像查看器中显示我为用户上传的图像
导航标题
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
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_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/user"
app:border_color="@android:color/white"
app:border_ />
<TextView
android:id="@+id/txtFullName"
android:layout_
android:layout_
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_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:background="@drawable/bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewA"
android:layout_
android:layout_
android:layout_weight="0.6"
android:background="@android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView"
android:layout_
android:layout_
android:layout_weight="1"
app:srcCompat="@drawable/no_image_user" />
</LinearLayout>
<LinearLayout
android:id="@+id/viewB"
android:layout_
android:layout_
android:layout_weight="0.4"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_
android:layout_
android:layout_weight="1"
android:text="Name"
android:textSize="25sp"
/>
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_
android:layout_
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_
android:layout_
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"/>
这是我的日志猫
致命异常:主要 进程:com.example.ytam.fonetegitim,PID:9482 java.lang.RuntimeException:无法启动活动 组件信息com.example.ytam.fonetegitim/com.example.ytam.fonetegitim.UserProfileActivity: java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.support.design.widget.NavigationView.getHeaderView(int)' 空对象引用 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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)' 空对象引用 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
【问题讨论】:
我编辑有问题 在此处添加您的 xml 文件。 编辑有问题 请检查我的回答。 共享 xml 布局,其中包括您的 NavigationView 【参考方案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);
【讨论】:
@YıldırımTAM 有什么问题。 @YıldırımTAM 请检查我的 EDIT ans 并告诉我 uri 的日志。 Error:(76, 58) error: incompatible types: UserProfileActivity cannot be convert to OnNavigationItemSelectedListener get this error on the line View headerView = navigationView.getHeaderView(0); @YıldırımTAM 请检查我的EDIT NEW ans。 我使用你的最后一次编辑 new 并且 R.id.profile_image 有一个错误,错误是“Not a statement”【参考方案2】:试试下面的代码
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();
);
【讨论】:
【参考方案3】:通过这种方法,我将 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);
【讨论】:
以上是关于如何在另一个活动中从 uri 设置导航抽屉查看器的图像的主要内容,如果未能解决你的问题,请参考以下文章