2

I'm fetching image from my android assets folder and cache.

My asset uri is //assets/initial/initialone.jpg while my gallery uri is /data/user/0/packagename/image/image.jpg.

I can't give these URI to the Ucrop android library.

Below is what I've done by far:

File cachePath=new Flie(getCacheDir(),"images")
if(!cachePath.exists())
cachePath.mkdirs();
FileOutputStream fos=new FileOutputStream(cachePath+"/image.jpg");
myImageBitmap.compress(Bitmap.CompressFormat.JPEG,80,fos);
fos.close();
Uri source=Uri.parse(cachePath+"/image.jpg");
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
UCrop.of(source,destination).start(this);

If I give this Uris then I get these errors:

 java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                at java.lang.Thread.run(Thread.java:818)
                                                                             Caused by: java.lang.IllegalArgumentException: Invalid Uri schemenull
                                                                                at com.yalantis.ucrop.task.BitmapLoadTask.processInputUri(BitmapLoadTask.java:184)
                                                                                at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:90)
                                                                                at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:41)
                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
Vimal
  • 197
  • 1
  • 11
  • `can't give these URI to the Ucrop android library.`. Of course you can. Of course you can pass that uri to that library. What is your problem? – greenapps Oct 29 '17 at 10:56
  • So you can give that uri to the library isnt it? Otherwise the library would not complain! – greenapps Oct 29 '17 at 16:58
  • Lets begin at the begin `cachePath.mkdirs();`. You are not checking the return value. If mkdirs() fails you continue as if nothing had happened. – greenapps Oct 29 '17 at 16:59
  • cache path also makes the dir... If I am sharing the same uri in other applications ex:watsapp..then the applications receive the image which is from the cache path. – Vimal Oct 29 '17 at 17:07
  • If the lib is working for you please post the code as answer – Vimal Oct 29 '17 at 17:09
  • Moreover you should only call mkdirs if the directory does not exist yet. – greenapps Oct 29 '17 at 17:09
  • I already said, the asset url can be shared with other apps.Then I don't know why you are asking me that comment "mkdir" – Vimal Oct 30 '17 at 11:06
  • That is a matter of good reading and then adapting your code. I wonder why you dont understand that you make wrong use of mkdirs. – greenapps Oct 30 '17 at 11:09
  • Reading is indeed a problem i see as you only did half of what i asked you to do. Repeat: `Lets begin at the begin cachePath.mkdirs();. You are not checking the return value`. – greenapps Oct 30 '17 at 11:16
  • but why I check that value – Vimal Oct 30 '17 at 11:17
  • That i already told you too. You can read it there. Why is reading so problematic? – greenapps Oct 30 '17 at 11:19

1 Answers1

4

Try this:

Uri url=Uri.fromFile(new File(("yourImagePath")));

I faced the same problem and it's working now.

Amitabh Sarkar
  • 1,281
  • 1
  • 13
  • 26