简体   繁体   English

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

[英]Can not load an image from url using Picasso?

I am trying to load an image using Picasso but nothing renders. 我正在尝试使用毕加索加载图像,但没有任何渲染。

The idea of the code is to choose an image from the device. 代码的想法是从设备中选择图像。 Later we upload it to the server and show it to the user. 稍后,我们将其上传到服务器并显示给用户。

First two parts are implemented, but the problem is image doesn't show in the corresponding image view. 前两部分已实现,但问题是图像未在相应的图像视图中显示。

Here is layout file- 这是布局文件-

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

MainActivity Class: MainActivity类:

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();
             }
           });
        }
    }
}

In the app level gradle file, I am using Picasso version 2.5.2- 在应用程序级别的gradle文件中,我正在使用Picasso 2.5.2-

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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'
    implementation 'com.google.firebase:firebase-core:16.0.8'
    implementation 'com.google.firebase:firebase-auth:16.2.0'
    implementation 'com.google.firebase:firebase-database:16.1.0'
    implementation 'com.google.firebase:firebase-storage:16.1.0'
   // implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.squareup.picasso:picasso:2.5.2'


/// 
 dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

I assume the problem is because you are not retrieving the url properly. 我认为问题是因为您没有正确检索URL。 After you upload the image inside the onSuccess try doing this instead: 在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...

Upon further inspection i have noticed that your onActivityResult may also have a problem. 经过进一步检查,我发现您的onActivityResult也可能有问题。 This does not seem right to me. 这对我来说似乎不正确。

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

Instead it should be: 相反,它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM