RuntimeException: Fail to connect to camera service using Fotoapparat in fragment - java

Need help to resolve a Fotoapparat library issue.
io.fotoapparat.exception.camera.CameraException: Failed to open camera with lens position: io.fotoapparat.characteristic.LensPosition$Back#31c3b8a2 and id: 0
When I open the first time It will work correctly. But after that, I click on other tabs and then open camera screen It will display a blank white screen. I am using viewPager for that.
How did you initialize FA?
mCameraView = view.findViewById(R.id.camera);
fotoapparat = Fotoapparat.with(getActivity())
.into(mCameraView)
.previewScaleType(ScaleType.CenterCrop)
.logger(new Logger() {
#Override
public void log(String s) {
Log.e("Camera", s);
}
#Override
public void recordMethod() {
}
})
.cameraErrorCallback(new CameraErrorListener() {
#Override
public void onError(CameraException e) {
e.printStackTrace();
fotoapparat.stop();
}
})
.build();
FA version: 2.5.0
Devices/APIs affected: 19,20,21,22

Related

How to display an image after a gif

I am new to android. I implemented a GIF in my activity.
I found a way to make it loop only once. So far everything is fine but I would like to display the image of this gif at the end of the animation of this one, this time in image and not in gif. Basically I would like the gif to loop once and then become an image again
I implemented this on my build.gradle :
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
After that I set up my imageView :
<ImageView
android:id="#+id/fuse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/_4sdp"
android:layout_marginLeft="#dimen/_4sdp"
android:padding="#dimen/_8sdp"
android:src="#drawable/fuseev4"
tools:ignore="InvalidId" />
Now this is my java file to loop once:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityChatRoomBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
imageView = binding.fuse;
try {
setListeners();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void setListeners() throws InterruptedException {
binding.LayoutSend.setOnClickListener(view -> {
try {
Glide.with(this)
.asGif()
.load(R.drawable.fuseev4) //Your gif resource
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.listener(new RequestListener<GifDrawable>() {
#Override
public boolean onLoadFailed(#Nullable #org.jetbrains.annotations.Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
return false;
}
})
.into(imageView);
}
catch (Exception e){
System.out.println(e);
}
afterListeners();
});
}
public void afterListeners(){
Glide.with(this)
.asBitmap()
.load(R.drawable.fuseev4)
.into(imageView);
}
I couldn't find a way to solve my problem.
Thanks in advance for your help !
Try this in your onResourceReady() callback:
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
#Override
public void onAnimationEnd(Drawable drawable) {
super.onAnimationEnd(drawable);
// your code here
}});
1)as i read you are using bumptech.glide liabrary so try code like below,
private val image = "abc.png"
Glide.with(this).load(image).placeholder(R.drawable.placeholder) .into(binding.imageView); //here loading gif
comment if any query

how to disable screenshot in capacitor plugin only for few pages not for entire app

it's an ionic capacitor angular project in which I am trying to build a capacitor plugin so that I can disable screenshots only for required pages/screens.
I have used getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); within MainActivity inside onCreate() method, it works but it disables the screenshot for entire application which is not the desired outcome.
MainAcitivity.java:
public class MainActivity extends BridgeActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
// Initializes the Bridge
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
// Additional plugins you've installed go here
// Ex: add(TotallyAwesomePlugin.class);
add(Contacts.class);
add(Screenshot.class);
}});
}
}
Now I have capacitor plugin "screenshot-plugin" in which I have 2 methods to "SET" and "CLEAR" the flag for particular pages/screen in project.
Screenshot.java:
#NativePlugin
public class Screenshot extends Plugin {
#PluginMethod
public void echo(PluginCall call) {
String value = call.getString("value");
JSObject ret = new JSObject();
ret.put("value", value);
call.success(ret);
}
#PluginMethod
public void enableScreenshot(PluginCall call) {
try {
Activity activity = getBridge().getActivity();
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
} catch (Exception e) {
Log.e("ABC", "Exception in enableScreenshot", e);
}
JSObject ret = new JSObject();
ret.put("status", true);
call.success(ret);
}
#PluginMethod
public void disableScreenshot(PluginCall call) {
try {
Activity activity = getBridge().getActivity();
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
} catch (Exception e) {
Log.e("ABC", "Exception in disableScreenshot", e);
}
JSObject ret = new JSObject();
ret.put("status", true);
call.success(ret);
}
}
Here I get an exception that only thread which created the view can modify it.
So I tried using MainActivity activity = new MainActivity() so that I can call getWindow() method on this activity but this gives an error cannot resolve symbol even when package is imported com.abc.myapp.
Also, when I try to use getWindow() without activity inside screenshot plugin then AndroidStudio gives compilation error, using getBridge().getActivity() and then calling getWindow() method (as in code) removes compilation error but gives above exception of only MainActivity can do so.
I could write these 2 methods in MainActivity itself but then not sure how to access these methods in ionic project component.
Using plugin I can call these methods inside my component but how to make this work for only few components/pages/screens and not for the entire android application.
Please help, Thanks in advance.
I know similar questions are already there but their use case and mine are different.
You can do like this in page where you want to disable:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
And remove flag from page where you don't want:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
Note: Not 100% sure but may be it give some idea.
found the answer to this, I built my own capacitor plugin and it works fine, but there is another plugin that is already published in the capacitor community and it supports IOS too, so check this out https://github.com/capacitor-community/privacy-screen
for those of you who want to know how it worked successfully, you have to put your code like this:
#PluginMethod
public void enableScreenshot(PluginCall call) {
final Activity activity = getBridge().getActivity();
final JSObject ret = new JSObject();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
} catch (Exception e) {
Log.e("ABC", "Exception in enableScreenshot", e);
ret.put("status", false);
}
}
});
if (!ret.has("status")) {
ret.put("status", true);
} else {
call.reject("Exception in enableScreenshot");
}
call.success(ret);
}
#PluginMethod
public void disableScreenshot(PluginCall call) {
final Activity activity = getBridge().getActivity();
final JSObject ret = new JSObject();
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
// Toast.makeText(activity, "Screenshot not allowed on this page.", Toast.LENGTH_SHORT);
} catch (Exception e) {
Log.e("ABC", "Exception in disableScreenshot", e);
ret.put("status", false);
}
}
});
if (!ret.has("status")) {
ret.put("status", true);
} else {
call.reject("Exception in enableScreenshot");
}
call.success(ret);
}
activity.runOnUiThread() is the hero.

Animated Vector drawable animation not working properly

I want to run AnimatedVectorDrawable animation on infinite loop.
final AnimatedVectorDrawableCompat avd = AnimatedVectorDrawableCompat.create(this, R.drawable.avd_anim_happy);
mImageView.setImageDrawable(avd);
// animation on infinite loop
avd.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
#Override
public void onAnimationStart(Drawable drawable) {
super.onAnimationStart(drawable);
Log.e(TAG, "onAnimationStart() called");
}
#Override
public void onAnimationEnd(Drawable drawable) {
Log.e(TAG, "onAnimationEnd() called");
avd.start();
}
});
avd.start();
}
This works fine when I tested with Nougat device. But when I try to run this on KitKat device, callback is called but animation is not started again. There is no
"onAnimationStart() called" log message. But upon doing the following.
#Override
public void onAnimationEnd(Drawable drawable) {
Log.e(TAG, "onAnimationEnd() called");
if (avd.isRunning()) {
Log.e(TAG, "avd running!!");
avd.stop();
}
avd.start();
}
onAnimationEnd() and onAnimationStart() are getting called again and again. But for some reason animation just stopped after playing for one time.
I was having the same issue with old platforms, turns out i work around it by posting the start after the end event.
imageView.post(new Runnable() {
#Override
public void run() {
avd.start();
}
});

Android camera works for one time only after giving permission

I try to show the content of the built-in camera inside my android device using ArCore. I have handled a code for asking permission at the beginning of the application. If I provide the permission I can see the current image stream coming from the camera.
After relaunching the application the result is black. The only solution is to revoke the camera permission inside the setting. Then the application asks again for permission and shows the image stream on the screen.
Is anybody who face the same problem? Do I have to refresh the permission? If I do RequestCameraPermission(); without checking permission, the activity is paused and resumed in a cycle. But I can see the first frame.
In the console the following error appears:
D/ACameraDevice: Device error received, code 3, frame number 51, request ID 0, subseq ID 0
and android_camera.cc:1088 Camera capture failed! frame: 51 reason: 1.
Code 3 is ERROR_CAMERA_DISABLED depending on https://github.com/aosp-mirror/platform_development/blob/master/ndk/platforms/android-24/include/camera/NdkCameraDevice.h
After some time only
I/native: analytics_logger.cc:190 The AnalyticsClient.sendAnalyticsMessage() method returned false. Will retry...
E/native: analytics_logger.cc:198 Could not send event. Event will be dropped.
is called in a loop. There is no activity anymore.
My code:
public class GameActivity extends NativeActivity
{
static GameActivity s_Instance;
public static GameActivity GetInstance()
{
return s_Instance;
}
#Override
public void onCreate(Bundle _SavedInstanceState)
{
super.onCreate(_SavedInstanceState);
s_Instance = this;
nativeInitializeInterface(getApplicationContext());
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onResume()
{
super.onResume();
if (!HasCameraPermission())
{
RequestCameraPermission();
return;
}
RequestCameraPermission();
}
#Override
protected void onPause()
{
super.onPause();
}
#Override
public void onStop()
{
super.onStop();
}
#Override
public void onDestroy()
{
super.onDestroy();
}
#Override
public void onRequestPermissionsResult(int _RequestCode, String[] _Permissions, int[] _Results)
{
if (!HasCameraPermission())
{
Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG).show();
if (!ShouldShowRequestPermissionRationale())
{
// Permission denied with checking "Do not ask again".
LaunchPermissionSettings();
}
finish();
}
}
public int GetDeviceOrientation()
{
return getResources().getConfiguration().orientation;
}
public int GetDeviceRotation()
{
return getWindowManager().getDefaultDisplay().getRotation();
}
public int GetDeviceDimensionWidth()
{
Point Size = new Point();
getWindowManager().getDefaultDisplay().getSize(Size);
return Size.x;
}
public int GetDeviceDimensionHeight()
{
Point Size = new Point();
getWindowManager().getDefaultDisplay().getSize(Size);
return Size.y;
}
public boolean HasCameraPermission()
{
return this.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
}
public void RequestCameraPermission()
{
this.requestPermissions(new String[] {Manifest.permission.CAMERA}, 0);
}
public boolean ShouldShowRequestPermissionRationale()
{
return this.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA);
}
public void LaunchPermissionSettings()
{
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", this.getPackageName(), null));
this.startActivity(intent);
}
public native void nativeInitializeInterface(Context context);
static
{
System.loadLibrary("app_droid");
}
}
Where exactly do you have the logic for presenting the image on screen? Maybe you only have it inside the permission granted callback.
(I'd put this answer as a comment, but can't yet due to reputation)

Is there such a thing as too many chromecast devices on one network?

I have an app that uses the CastCompanionLibrary and I'm having a weird issue. On a small network with one chromecast device my application is able to detect it and show the MediaRouterItem.
Although, I had a beta tester say that they were not able to get any of their devices detected, so the icon never shows up for them. Come to find out they are connected to a larger, shared network with multiple chromecast devices connected to it. They said that they are able to detect all of the chromecast devices with other apps such as YouTube and Localcast though. Which is weird because that leads me to believe that maybe I'm not doing something right with the discovery process.
Unfortunately, I am not in a position where I can have a network with multiple chromecast devices to debug this issue, so I was just wondering if anyone else had a similar issue? Or is there a certain method that you have to call with the CastCompanionLibrary that I'm missing?
EDIT
I am using the APP_ID that has been published, so I know that it isn't a whitelisting issue.
The code I use for discovery is completed by the CastCompanionLibrary. This is what I use once the onCastDeviceDetected() callback is called:
mCastConsumer = new VideoCastConsumerImpl() {
#Override
public void onFailed(int resourceId, int statusCode) {
}
#Override
public void onConnectionSuspended(int cause) {
Log.d(TAG, "onConnectionSuspended() was called with cause: " + cause);
}
#Override
public void onConnectivityRecovered() {
}
#Override
public void onCastDeviceDetected(final MediaRouter.RouteInfo info) {
if (!MyApplication.isFtuShown(Home.this)) {
MyApplication.setFtuShown(Home.this);
Log.d(TAG, "Route is visible: " + info);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (mediaRouteMenuItem.isVisible()) {
Log.d(TAG, "Cast Icon is visible: " + info.getName());
//showFtu();
}
}
}, 1000);
}
}
};
MyApplication.class:
public static boolean isFtuShown(Context ctx) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
return sharedPref.getBoolean(FTU_SHOWN_KEY, false);
}
public static void setFtuShown(Context ctx) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx);
sharedPref.edit().putBoolean(FTU_SHOWN_KEY, true).commit();
}

Categories