Search code examples
androidimageviewandroid-volleycropwallpaper

How to get the Uri path from a ImageView?


I am trying to create a wallpaper application. All images will come from internet.

Before of set as background, I need to crop the image. And for this, I am using an API named AndroidImageCropper. This API need of a URI object to execute the crop.

How can I get the Uri from ImageView? Because my image is from internet. The image is not on drawable folder. I am confusing. Maybe you guys can help me.

public class MainActivity extends AppCompatActivity {

private NetworkImageView imageView;
private Uri mCropImageUri;
public static final String URL_PHOTO = "http://cdn.wonderfulengineering.com/wp-content/uploads/2016/02/iron-man-wallpaper-42-610x1084.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = (NetworkImageView) findViewById(R.id.image_view);

    // Singleton for Volley API, Load image from internet
    ImageLoader loader = MySingleton.getInstance(this).getImageLoader();
    loader.get(URL_PHOTO, ImageLoader.getImageListener(imageView, R.drawable.temp_picture, android.R.drawable.ic_dialog_alert));

    imageView.setImageUrl(URL_PHOTO, loader);

    // Put the image on URI object
    // #################################
    // ###    mCropImageUri =  <--   ###
    // #################################

}

// Action for my button
public void cropImage(View view) {
    // API to crop an image
    CropImage.activity(mCropImageUri)
            .start(this);
}

}

Do you have any suggestion to me? Thanks.


Solution

  • So you're using Volley? I'm not very familiar with Volley, both just some of my thoughts.

    After image is downloaded to Android device, try to find the absolute path of the file and use android.net.Uri.fromFile to get a uri from the image.

    Or else, can you use Bitmap object directly, instead of an uri.


    OK, after some investigation, I think you have two solutions.

    1. Using volley to download image as file, without using ImageLoader. In this way it's easy to get file uri of the image.
    2. For Android-Image-Cropper, you can use Activity or use View(refer here). You can get the Bitmap object of the image in method onLoadingComplete of ImageLoadingListener. Then use com.theartofdev.edmodo.cropper.CropImageView to crop the image.