Glide download image and save as file synchronously - java

I'm trying to download an image from a URL using Glide and get the path of the file and forward it to WallpaperManager.getCropAndSetWallpaperIntent to be set as a wallpaper.
I found that this can be done using asFile method of Glide
Kotlin:
val data = Glide
.with(context)
.asFile()
.load(url)
.submit()
But when I call data.get() I get the error
java.lang.IllegalArgumentException: You must call this method on a background thread
So followed this answer and implemented MyAsyncTask
interface AsyncResponse {
fun processFinish(output: File?)
}
class MyAsyncTask(delegate: AsyncResponse) : AsyncTask<FutureTarget<File>, Void, File?>() {
override fun doInBackground(vararg p0: FutureTarget<File>?): File? {
return p0[0]?.get()
}
private var delegate: AsyncResponse? = null
init {
this.delegate = delegate
}
override fun onPostExecute(result: File?) {
delegate!!.processFinish(result)
}
}
And I'm doing this now
fun getFile(context: Context, url: String) : File {
val data = Glide
.with(context)
.asFile()
.load(url)
.submit()
val asyncTask = MyAsyncTask(object : AsyncResponse {
override fun processFinish(output: File?) {
println(output?.path)
}
}).execute(data)
return asyncTask.get()
}
But I can't seem to get the File
Edit:
It was working but now there's a new error
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.service.wallpaper.CROP_AND_SET_WALLPAPER dat=content://com.rithvij.scrolltest.provider/cache/image_manager_disk_cache/efebce47b249d7d92fd17340ecf91eb6b7ff86f91d71aabf50468f9e74d0e324.0 flg=0x1 pkg=is.shortcut }
Full stack trace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rithvij.scrolltest, PID: 2760
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.service.wallpaper.CROP_AND_SET_WALLPAPER dat=content://com.rithvij.scrolltest.provider/cache/image_manager_disk_cache/efebce47b249d7d92fd17340ecf91eb6b7ff86f91d71aabf50468f9e74d0e324.0 flg=0x1 pkg=is.shortcut }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1816)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.Activity.startActivityForResult(Activity.java:4396)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4355)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivity(Activity.java:4679)
at android.app.Activity.startActivity(Activity.java:4647)
at com.rithvij.scrolltest.MainActivity$onCreate$1.onClick(MainActivity.kt:71)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
Now my question is is this the preferred way to set wallpaper from a url?
How to deal with the other error?

Regarding your first question about getting image from url, instead of using asFile, it is recommended that you use the method downloadOnly(). Then, rather than using an AsyncTask, you can leverage a RequestListener to get an async callback when the resource is loaded.
As for your second question, you are broadcasting an Implicit Intent that is not registered by the OS or any apps on your device. Rather than broadcasting an intent, you can try leveraging the WallpaperManager System Service.

Answering my own question
It is better to use downloadOnly() as suggested by Elli White here.
But I wasted enough time on researching this question and I got a working solution so I decided not to start from scratch.
The error I got was because of the image file name that's being returned by Glide.
I fixed it by copying the file somewhere and using it as the source.
val file = asyncTask.get()
// copy file
val tempFile = File.createTempFile("image", ".png")
copyFile(file!!.inputStream(), FileOutputStream(tempFile))
And for my use case i.e. to set the image as wallpaper I need not worry about the file extension as long as I specify that it's an image .png in this case.

Related

Upload Image/ImageProxy class to a Server in Android with HTTP2

I am trying to capture an ImageCapture with the camera and upload to an API without saving it in the device; but I am unable to find any straightforward way to upload the Image class. And, whether I should convert the image to bytesarray and POST it into the API where it can be handled. What I need are some resources or examples on how to do. Here is my code, where uploadFile is going to be the function where we'll pass an ImageProxy object:
...
val imgCap = ImageCapture(imageCaptureConfig)
findViewById<View>(R.id.screen).setOnClickListener { _: View? ->
imgCap.takePicture(object : ImageCapture.OnImageCapturedListener() {
override fun onCaptureSuccess(image: ImageProxy, rotationDegrees: Int) {
Toast.makeText(baseContext, "Image Captured", Toast.LENGTH_SHORT).show()
uploadFile(image)
super.onCaptureSuccess(image, rotationDegrees)
}
})
}
...

How can I access the streaming camera video as bitmap or ByteBuffer in Android studio without saving it?(Java)

I am building an app in Android studio in Java where I want to access realtime video from camera without saving it and get it as a ByteBuffer. Any help will be appreciated.
It Depends on the API you use (CameraX, Camera2).
If you use Camera2 API the general flow would be:
Open a Camera:
val manager = getSystemService(CAMERA_SERVICE) as CameraManager
manager.openCamera(cameraId, cameraStateCallback, cameraHandler)
Wait for the camera to open:
private val cameraStateCallback: CameraDevice.StateCallback = object : CameraDevice.StateCallback() {
override fun onOpened(cameraDevice: CameraDevice) {
//Start a capture session
}
override fun onDisconnected(cameraDevice: CameraDevice) {
cameraOpenCloseLock.release()
cameraDevice.close()
}
override fun onError(cameraDevice: CameraDevice, error: Int) {
cameraOpenCloseLock.release()
cameraDevice.close()
}
}
Then start a capture session, and using ImageReader to receive images.
You can check this project for a small basic working example of the entire flow.

Camera2 - "Must be called from main thread of fragment host" when changing fragment

I'm trying to change the fragment after an image is taken with the following code Google Sample - Camera2Basic.
I've implemented a callback to my MainActivity at line 839 of the above sample. However when I am trying to traverse to a different activity from that callback I receive the following exception:
java.lang.IllegalStateException: Must be called from main thread of
fragment host
Does anyone know anyway around this?
I have the working code in Kotlin
You must replace this callback with:
val captureCallback = object : CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult) {
sendBackResult(mFile)
}
}
mCaptureSession!!.capture(captureBuilder.build(), captureCallback, mBackgroundHandler)
} catch (e: CameraAccessException) {
e.printStackTrace()
}
sendBackResult method is as follows:
private fun sendBackResult(resultFile: File?) {
val fileUri = Uri.fromFile(resultFile)
val dataIntent = Intent()
dataIntent.data = fileUri
dataIntent.putExtra("isFront", isFrontCamera)
activity!!.setResult(Activity.RESULT_OK, dataIntent)
activity!!.finish()
}

How can I DetectFaces in Amazon Rekognition AWS with Android Studio?

I have tried so many way but i can't succeed. I haven't found any source code examples for Android(about rekognition)
there's a source code in JAVA in the Developer Guide but i cannot implement that even though I tried TT
I try to detect faces by sending an image file from an external storage(from the emulator)
I don't know what i did wrong(I'm not good at coding)
Here is my code
AmazonRekognitionClient amazonRekognitionClient;
Image getAmazonRekognitionImage;
DetectFacesRequest detectFaceRequest;
DetectFacesResult detectFaceResult;
File file = new File(Environment.getExternalStorageDirectory(),"sungyeol.jpg.jpg");
public void test_00(View view) {
ByteBuffer imageBytes;
try{
InputStream inputStream = new FileInputStream(file.getAbsolutePath().toString());
imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
Log.e("InputStream: ",""+inputStream);
Log.e("imageBytes: ","");
getAmazonRekognitionImage.withBytes(imageBytes);
// Initialize the Amazon Cognito credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"us-east-2:.......", // Identity Pool ID
Regions.US_EAST_2 // Region
);
//I want "ALL" attributes
amazonRekognitionClient = new AmazonRekognitionClient(credentialsProvider);
detectFaceRequest = new DetectFacesRequest()
.withAttributes(Attribute.ALL.toString())
.withImage(getAmazonRekognitionImage);
detectFaceResult = amazonRekognitionClient.detectFaces(detectFaceRequest);
detectFaceResult.getFaceDetails();
}
catch(Exception ex){
Log.e("Error on something:","Message:"+ex.getMessage());
}
and here is my errors
02-04 09:30:07.268 29405-29405/? E/InputStream:: java.io.FileInputStream#a9b23e7
02-04 09:30:07.271 29405-29405/? E/Error on something:: Message:Attempt to invoke virtual method 'com.amazonaws.services.rekognition.model.Image com.amazonaws.services.rekognition.model.Image.withBytes(java.nio.ByteBuffer)' on a null object reference
what is a null object reference?
i try to change the file path but he said no such file ... and when I change to this path, there's errors above.
by the way I've already asked a user for a permission to access a folder from Emulator in Android
please help me
PS. sorry for my bad English
Thank you in advance.
Now I am ok with the issues. I have been through many many things <3 <3 <3.
Thank you
I'm Thai and I had to try harder to find the solutions because there's lack of information in the particular language. Here are my solutions.
My solutions are:
0.There is an endpoint for setting for the Rekognition-->
http://docs.aws.amazon.com/general/latest/gr/rande.html#rekognition_region
1.On a "null object reference issue" I found that I have to create a new object first such as "Image image = new Image();" <-- The "new" command creates an object instance in that class
2.After the above error, there are more errors (Errors on NetworkOnMainThreadException), so I tried everything until I found this page -->
https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html the page said that ...
Consequently, I looked up for more information about the AsyncTask and after that I created an AsyncTask class and then I move all my code about the initialize, the request, the response to the AsyncTask class. ตอนรันตอนท้ายๆน้ำตาจิไหล my code worked... TT and by the conclusion the sungyeol.jpg.jpg file worked
for example
private void testTask(){
.... all code in the main thread particularly on the requests and responses
from the services
//print the response or the result
//Log.e() makes the message in the android monitor red like an error
Log.e("Response:", [responseparameter.toString()]);
}
//create the inherited class from the AsyncTask Class
//(you can create within your activity class)
class AsyncTaskRunner extends AsyncTask<String,String,String>{
#Override
public String doInBackground(String ... input){
testTask(); // call the testTask() method that i have created
return null; // this override method must return String
}
}
//I've created a button for running the task
public void buttonTask(View view){
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
}
for more information about the AsyncTask:
https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask
http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.WJdkqVOLTIU
I hope these help :)

Android WebView getFavicon() returning null

I'm trying to get the favicon of the loaded page after using
WebView webView = new WebView(getActivity());
webView.loadUrl("http://" + url);
I'm attaching the asynchronous WebViewClient to the WebView to get the favicon after it loads
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(WebView view, String url)
{
String linkTitle = view.getTitle();
Bitmap favicon = view.getFavicon();
onLinkUrlFinished(url, linkTitle);
}
});
The favicon getting back is always null, even for websites such as google/facebook that has favicons for sure.
Another thread says to use WebIconDatabase but it's deprecated:
Display the Android WebView's favicon
The API on android site refers to WebViewClient.onReceivedIcon which doesnt even exist.http://developer.android.com/reference/android/webkit/WebView.html#getFavicon%28%29
What's going on here?
In order to use onReceiveIcon(), you should use setWebChromeClient.
This is what I do and it's working for me.
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
webImage.setImageBitmap(icon);
}
});
WebIconDatabase is deprecated as of API 19. According to the comments in the code:
#deprecated This class is only required when running on devices up to
{#link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}
So unless you don't want to support API 18 and below, you should still be using WebIconDatabase:
WebIconDatabase.getInstance().open(getDir("icons", MODE_PRIVATE).getPath());
And then, regardless what API you want to support, you need to specify in a custom WebChromeClient:
public class MyCustomWebChromeClient extends WebChromeClient {
#Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
// do whatever with the arguments passed in
}
}
Remember to register your custom WebChromeClient with your WebView:
mWebView.setWebChromeClient(new MyCustomWebChromeClient());
The key is to open the WebIconDatabase so WebView has somewhere to put the icons, and override WebChromeClient.onReceivedIcon. For additional information, see this StackOverflow article.
I know its an old thread but, for those facing problems getting favicon using webview client.
Kotlin:
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
tabTitle.text = view?.title // read website title
loadImg(view) // method to load the favicon
}
private fun loadImg (view: WebView?){
// u can directly use tabImage.setBitmap instead of assigning tabImg as val
val tabImg: ImageView = findViewById(R.id.tabImage)
// creating handler object to delay the associated thread a little bit after onPageFinished is called.
val handler = Handler()
val runnable = Runnable {
if(view?.favicon != null) {
tabImg.setImageResource(0) //remove the default image
tabImg.setImageBitmap(view?.favicon) // set the favicon
}
}
handler.postDelayed(runnable, 200) // delay time 200 ms
}
It worked for me, hope it helps new readers, plz up vote if it helps u, so that u can help others!
Best regards
So in the end I didn't end up using the deprecated API, instead I found out that if you put /favicon.ico after the domain, it'll give you the ico file, which I used in the end to fetch the image. The Uri API will have a getHost() method that will give you the host without having to manually parse it
String faviconUrl = Uri.parse(url).getHost() + "/favicon.ico";
For google for example the icon url will be www.google.com/favicon.ico

Categories

Resources