【问题标题】:How can you load image string paths and image files with picasso and glide?如何使用 picasso 和 glide 加载图像字符串路径和图像文件?
【发布时间】:2016-09-29 14:01:43
【问题描述】:

所以无论我做什么,picasso 和 glide 都不能将图像文件或图像字符串路径加载到图像视图中。

我试过了。

这是我的代码:

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


        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
        {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            user_image_path = cursor.getString(columnIndex);//here we have our image path.
            cursor.close();

            File tempFile2 = new File(selectedImage.getPath());

            //Toast.makeText(this, "I got your image " + user_image_path, Toast.LENGTH_SHORT).show();
            myImageView = (ImageView) findViewById(R.id.cameraActivity_ImageView);
            //Picasso.with(TakePhotoActivity.this).load(selectedImage).fit().centerCrop().into(myImageView);
            Glide.with(this).load(new File(selectedImage.getPath())).centerCrop().into(myImageView);


        }



    }

所以我试过了:

1)Picasso.with(TakePhotoActivity.this).load(user_image_path).fit().centerCrop().into(myImageView);

2)Glide.with(this).load(user_image_path).centerCrop().into(myImageView);

3)Picasso.with(TakePhotoActivity.this).load(tempFile2).fit().centerCrop().into(myImageView);

4)Glide.with(this).load(tempFile2).centerCrop().into(myImageView);

5)Picasso.with(TakePhotoActivity.this).load(selectedImage.getPath())).fit().centerCrop().into(myImageView);

6)Glide.with(this).load(selectedImage.getPath())).centerCrop().into(myImageView);

他们都没有工作。如果我使用:

Picasso.with(TakePhotoActivity.this).load(selectedImage).fit().centerCrop().into(myImageView);

Glide.with(this).load(selectedImage).centerCrop().into(myImageView);

上面这两个与 Uri selectedImage 变量一起使用,但其他都不起作用。那么为什么其他选项不起作用以及如何让它们起作用,因为理想情况下我想使用图像路径字符串。

希望大家帮忙!我完全被这个难住了。

【问题讨论】:

    标签: android uri picasso android-glide android-photos


    【解决方案1】:
    Picasso.with(context).load(data.getData()).into(imageView);
    

    【讨论】:

    • 这不起作用,它一直说“用 String.valueOf() 包装 uri”
    • 试试这个 Picasso.with(context).load(data.getData()).into(imageView);
    • 是的 data.getData() 有效,但它与:Picasso.with(TakePhotoActivity.this).load(selectedImage).fit().centerCrop().into(myImageView);我想知道如何在 Picasso 或 glide 中使用字符串路径或 File 对象
    【解决方案2】:

    Glide 可以通过 Uri 完成,如下面的代码所述。 但你应该检查

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    如果你已经做过了,我不知道。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode  == RESULT_OK){
            if (requestCode == PIC_REQ_CODE){
                Glide.with(this).load(data.getData()).into((ImageView)findViewById(R.id.iv_result));
            }
        }
    }
    

    【讨论】:

    • 是的 data.getData() 有效,但它与:Picasso.with(TakePhotoActivity.this).load(selectedImage).fit().centerCrop().into(myImageView);我想知道如何在 Picasso 或 glide 中使用字符串路径或 File 对象
    • Glide 可以直接使用uri,但是picasso 不行。如果您想将 uri 更改为实际部分,请阅读此链接-stackoverflow.com/questions/6935497/android-uploading-image
    猜你喜欢
    • 2023-03-10
    • 2019-09-18
    • 2017-09-04
    • 2017-10-01
    • 2021-08-03
    • 2021-04-30
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多