我正在尝试从firebase存储中获取图像,并将其加载到导航标题上的圆形图像视图中。我用毕加索将文件设置到视图中,但它返回空值。。。我不知道为什么。
这就是我得到的错误:
03-15 05:42:37.178 23532-23532/com.example.lnf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lnf, PID: 23532
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lnf/com.example.lnf.MainActivity}: java.lang.IllegalArgumentException: Target must not be null.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:682)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:665)
at com.example.lnf.MainActivity.LoadImgToCircleView(MainActivity.java:97)
at com.example.lnf.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
主要活动:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private StorageReference profileImgRef;
private CircleImageView circleImageViewMain;
private TextView name, email;
String hearderImgUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initialize Firebase modules
firebaseAuth=FirebaseAuth.getInstance();
databaseReference= FirebaseDatabase.getInstance().getReference().child("users");
profileImgRef= FirebaseStorage.getInstance().getReference();
circleImageViewMain =findViewById(R.id.circleImageHeader);
name=findViewById(R.id.txtName);
email=findViewById(R.id.txtEmail);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
LoadImgToCircleView(CheckUserDataBase());
}
private void LoadImgToCircleView(String uid) {
try {
File localFile =File.createTempFile("profile","png");
StorageReference filepath=profileImgRef.child(uid).child("profileImg/profile.png");
filepath.getFile(localFile);
Picasso.get()
.load(localFile)
.placeholder(R.drawable.ic_account)
.into(circleImageViewMain);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser user=firebaseAuth.getCurrentUser();
if (user == null){
SendUserToLogin();
} else {
CheckUserDataBase();
}
}
private String CheckUserDataBase() {
final String userID =firebaseAuth.getCurrentUser().getUid();
databaseReference.addValueEventListener(
new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.child(userID).hasChild("userInfo")){
SendUserToSetup();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}
);
return userID;
}
private void SendUserToSetup() {
Intent i = new Intent(this,SetupActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
private void SendUserToLogin() {
Intent i = new Intent(this,LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_profile) {
} else if (id == R.id.nav_my_posts) {
} else if (id == R.id.nav_messeges) {
} else if (id == R.id.action_settings) {
} else if (id == R.id.nav_logout) {
firebaseAuth.signOut();
SendUserToLogin();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
navigation_header_main.xml
<LinearLayout 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="@drawable/login"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circleImageHeader"
android:layout_width="120dp"
android:layout_height="120dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/ic_account"
app:civ_border_color="@color/colorAccent"
app:civ_border_width="3dp" />
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/nav_header_title"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nav_header_subtitle" />
build.gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.lnf"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
android.defaultConfig.vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
api 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
implementation 'com.google.firebase:firebase-storage:16.1.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.firebaseui:firebase-ui-storage:4.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
首先在活动文件中更改初始化,如下所示
circleImageViewMain =(CircleImageView)findViewById(R.id.circleImageHeader);
name=(TextView)findViewById(R.id.txtName);
email=(TextView)findViewById(R.id.txtEmail);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
告诉我错误日志后,提交
然后用picaso移除你的图像,替换为
if(filepath.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(filepath.getAbsolutePath());
circleImageViewMain.setImageBitmap(myBitmap);
}
首先记录你的图像网址是来自Firebase还是不是之后,你必须这样做。
//Nav Header Section
//Here I am using glide you can use picasso
View header = navigationView.getHeaderView(0);
navImageView = header.findViewById(R.id.nav_ImageView);
navTextView = header.findViewById(R.id.nav_Textview);
navTextView.setText(sharedPreferences.getString("name", ""));
Log.d("success121", String.valueOf(navTextView));
navText = sharedPreferences.getString("logo",null);
if(navText!=null) {
Log.e("Photo Url: ", navText);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_launcher_background);
requestOptions.error(R.drawable.ic_launcher_foreground);
Glide.with(this).load(navText)
.apply(requestOptions).thumbnail(0.5f).into(navImageView);
}
在你的代码中,你并没有告诉编译器你的映像设置在哪里。根据您的代码,编译器尝试在活动而不是导航标题上设置图像。
我有下面的代码来加载毕加索的图像,在下载图像时使用可绘制的占位符来显示图像。不过,我想要的是一个动画旋转进度条样式的微调器,它可以在图像加载时不断地旋转,就像我在大多数专业应用程序中看到的那样。毕加索似乎不支持这一点,只支持静态图像绘制。有没有办法让它与毕加索合作,或者我必须做些不同的事情?
我可以使用Picasso库从文件系统加载图像吗? 我正在使用让用户从他的图库中选择一张照片,然后想要显示所选图像。 我已经有了获取图像文件系统Uri的工作代码,但无法获取毕加索。load()方法工作。
问题内容: 我想使用Picasso在列表视图中一个接一个地加载三个连续的图像。使用毕加索提供的方法可以轻松做到这一点。但是,由于这些图像在不同的时间加载,因此在图像进入时会产生闪烁效果。例如,有时图像2出现在图像1之前,而当图像1加载时会导致不自然的结结。如果可以将listview的可见性设置为不可见,直到可以显示所有图像,那将更好。但是,我找不到用于毕加索的回调方法,该方法会在加载图像时发出信号
我正在使用毕加索图书馆。我知道,如果我将图片从URL加载到图像视图中,有一种回调方法。但我不想加载到imageview中。相反,我想将其另存为位图。所以我用了下面的代码 我怎样才能得到一个回调方法,知道我的图像是成功下载使用毕加索? 不要说位图对象的空检查。这会导致错误。
以下是我的上传代码: 编辑:我将base64字符串复制到一个在线转换器中,看起来不错,它返回了正确的图像。看起来,数据没有问题。firebase的处理方式有问题。 编辑:我尝试显式地将类型设置为image/jpeg而不是image/jpg,如下所述:当Firestorage关闭时显示图像的正确方式,或者在Firestorage for iOS中加载预览时出错,但没有区别。