无法使用毕加索从URL加载图像?

时间:2019-03-29 17:45:26

标签: android firebase picasso

我正在尝试使用Picasso加载图像,但是没有渲染。

代码的想法是从设备中选择图像。稍后,我们将其上传到服务器并显示给用户。

实现了前两部分,但是问题是图像未在相应的图像视图中显示。

这是布局文件-

/n

MainActivity类:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  android:orientation="vertical"

    tools:context=".MainActivity">

    <Button
        android:id="@+id/b_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="Select image"
      />

    <ImageView
        android:id="@+id/profileImage"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_below="@+id/b_img"
        />
</LinearLayout>

在应用级gradle文件中,我使用的是Picasso 2.5.2-

public class MainActivity extends AppCompatActivity {


    private Button bSelectImg;
    private StorageReference storageReference;
    private static final int g_img = 1;
    private ProgressDialog progressDialog;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {....

        storageReference = FirebaseStorage.getInstance().getReference();
        progressDialog = new ProgressDialog(this);
        bSelectImg = (Button)findViewById(R.id.b_img);
        imageView =(ImageView)findViewById(R.id.profileImage);

        bSelectImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
         Intent intent= new Intent(Intent.ACTION_PICK);
         intent.setType("image/*");
         startActivityForResult(intent , g_img);
            }
        });
    }


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

        if(requestCode == g_img && requestCode != RESULT_OK ) {

            Uri uri = data.getData();

            progressDialog.setMessage("uploading.....");
            progressDialog.show();

            StorageReference filepath = storageReference.child("Photos").child(uri.getLastPathSegment());

            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    Task<Uri> downloadUrl = storageReference.getDownloadUrl();

                    Log.i("urlimagexcx",""+downloadUrl);

                    Picasso.with(MainActivity.this)
                            .load(downloadUrl.toString())
                            .into(imageView);

                  Toast.makeText(MainActivity.this,"Upload is don",Toast.LENGTH_LONG).show();
                  progressDialog.dismiss();
             }
           });
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题是因为您没有正确检索URL。 在onSuccess内部上传图片后,请尝试执行以下操作:

        filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(final Uri uri) {
                                String downloadUrl = uri.toString();

                               Picasso.with(MainActivity.this)
                               .load(downloadUrl)
                               .into(imageView);
                            //continue closing brackets here...

进一步检查后,我注意到您的onActivityResult也可能有问题。 这对我来说似乎不正确。

   if(requestCode == g_img && requestCode != RESULT_OK ) 

应该是:

   if(requestCode == g_img && resultCode == RESULT_OK )