Image Cropper Issue - java

So Im using the ArthurHub/Android-Image-Cropper. Im having an issue on getting the cropper to popup. So right now Ive got a screen where I click a button and it takes me to my gallery and that is working perfectly fine. I can go to my gallery and all that. But when I click the image from the gallery, instead of it taking me to the crop editor, I go right back to the activity I was just at. This is my code
public class TestShareActivity extends AppCompatActivity {
private ImageButton imageButton;
private EditText mCaption;
private Button mShareBtn;
private static final int GALLERY_REQUEST = 1;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_share);
imageButton = (ImageButton) findViewById(R.id.profileImageButton);
mCaption = (EditText) findViewById(R.id.addCaption);
mShareBtn = (Button) findViewById(R.id.uploadBtn);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST && requestCode == RESULT_OK){
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
}
}
I also have both of these in my Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Logcat from when I click the image button to go to my gallery to when I click the image I want which then takes me back to my activity
Suspending all threads took: 16.868ms
Recording user engagement, ms: 2309
Activity paused, time: 11770529
Logging event (FE): _e, Bundle[{_o=auto, _et=2309, _sc=TestShareActivity, _si=3274131924141379636}]
showStatusIcon on inactive InputConnection
Inactivity, disconnecting from the service
Using measurement service
Connecting to remote service
Activity resumed, time: 11775975
Connected to remote service
Timeline: Activity_idle id: android.os.BinderProxy#39c720c9 time:5790129
Processing queued up service tasks: 1
Inactivity, disconnecting from the service

Related

How to stop reload the data when user open second time Fragment or activity?

"Ex: In flipkart when we open App, Home screen will load the data and when we go to next activity from home screen and again we come back to Home screen its not loading again "
Cancel all the tasks using lifecycle methods of Fragment: "onPause", "onStop" and etc (you have to choose one of them). Fragment lifecycle.
Use onActivityResult
Define constant
public static final int REQUEST_CODE = 1;
Call your custom dialog activity using intent
Intent intent = new Intent(Activity.this,
CustomDialogActivity.class);
startActivityForResult(intent , REQUEST_CODE);
Now use onActivityResult to retrieve the result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//reload data here
}
} catch (Exception ex) {
Toast.makeText(Activity.this, ex.toString(),
Toast.LENGTH_SHORT).show();
}
}
In custom dialog activity use this code to set result
Intent intent = getIntent();
intent.putExtra("key", value);
setResult(RESULT_OK, intent);
finish();
While tap on bottom back button it will not reload, because it just finishes current activity and resumes home activity.
To prevent reload home activity while tap on back button from toolbar, you have to add
android:launchMode="singleTop"
to your home activity in your manifest file.
For more details refer here

How to disable back press action for an intent?

I have an Activity which makes use of KeyguardManager.
The intention is to disallow the user to use the app, if they are unable to successfully supply their credentials.
Though the keyguard intent appears at the start of the app, pressing the device back button moves the intent away, showing the activity which started it.
Overriding the onBackPressed does not seem to help, as it isn't associated with the intent.
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (km.isKeyguardSecure()) {
setShowWhenLocked(true);
Intent i = km.createConfirmDeviceCredentialIntent("Authentication required", "password");
startActivityForResult(i, CODE_AUTHENTICATION_VERIFICATION);
}
}
What if you use finish() after startActivity() ?
EDIT:
Add finish() on your onActivityResult() if the pattern is false.
What you want to achieve can be done using a "Staging" Activity. For example, you can have a LoginActivity that will check if the user is authenticate or not then from there decide where to redirect him.
The LoginActivity should look something like this, of course you need to adapt it to your business logic :
public class LoginActivity extends AppCompatActivity {
private static final int CODE_AUTHENTICATION_VERIFICATION = 24;
private boolean isFirstLaunch = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
isFirstLaunch = false;
//startActivityForResult With your intent to authenticate the user
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CODE_AUTHENTICATION_VERIFICATION){
Log.i("LOGIN", "return from key guard");
//Check the data and decide if you redirect the user to main activity or not
}
}
#Override
protected void onResume() {
super.onResume();
if(!isFirstLaunch){
Log.i("LOGIN", "resume not first launch");
// the user tried to cancel the authentication either present him with the authentication process again or finish() the activity
}
}
}
Please, N/B: Overriding the onBackPressed does help only when you create a conditional statement controlled by a boolean variable in the onBackPressed method and call it in the onActivityResult i.e when the resultCode != RESULT_OK. Another option is to exit the app when resultCode != RESULT_OK (moveTaskToBack(true)) Here is what I mean below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INTENT_AUTHENTICATE) {
if (resultCode == RESULT_OK) {
//do something you want when pass the security
} else //resultCode != RESULT_OK
//Option 1 Ensure you override onBackPressed() with a conditional
//statement controlled by a boolean variable.
onBackPressed();
//Option 2
moveTaskToBack(true); //Exit app when a user click the back button.
}
}

How to play a video from gallery in Android?

I am trying to play a video from the gallery, but i can only reach the gallery.
I have a button and when i press it i am redirected to the gallery. I can see that there are videos, but when i press the video the application crashes.
This is the "On activity result".
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == SELECT_PHOTO ) {
Uri mVideoURI = data.getData();
video_player_view.setMediaController(media_Controller);
video_player_view.setVideoURI(mVideoURI);
video_player_view.requestFocus();
video_player_view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
video_player_view.start();
}
});
}
}
And this is "On Create"
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_camera = (Button)findViewById(R.id.btn_camera);
Button btn_arhiva = (Button)findViewById(R.id.btn_arhiva);
video_player_view = (VideoView)findViewById(R.id.video_player_view);
media_Controller = new MediaController(this);
media_Controller.setAnchorView(video_player_view);
btn_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
});
btn_arhiva.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*,video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
The Exception:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=199, result=-1, data=Intent { dat=content://media/external/video/media/39 }} to activity {licenta.licenta/licenta.licenta.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setMediaController(android.widget.MediaController)' on a null object reference`
I searched online and tutorials are made using a video from "Raw" directory. But i want it dynamically. If i record a video now, i want to be able to play it through the app.
Thank you!

Google Glass Take Picture Intent

I'm trying to take a picture with a Google Glass App. Therefore I'm using a SurfaceView which shows the camera preview in the background. The photo is taken with an intent.
The problem is that the onActivityResult method belonging to the intent is never called. I've read that this problem is a bug on Google Glass but should be closed with the newer versions of Google Glass.
onCreate Method:
private CameraSurfaceView cameraView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiate CameraView
cameraView = new CameraSurfaceView(this);
// Set the view
this.setContentView(cameraView);
}
Intent Calling:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_camera:
take picture
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent != null)
{
Toast.makeText(getApplicationContext(), "Taking Picture",
Toast.LENGTH_SHORT).show();
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This all works fine. The user sees the camera preview and when the intent is called a picture will be taken and stored. After that the user sees a prompt "Tap to Accept". After tapping the app ends but the onActivityResult method is never called.
onActivityResult Method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i("Camera", "Hello from onActivityResult");
// Handle photos
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK)
{
String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
processPictureWhenReady(picturePath);
}
super.onActivityResult(requestCode, resultCode, data);
}
Releasing the camera:
#Override
protected void onResume() {
super.onResume();
// Do not hold the camera during onResume
if (cameraView != null) {
cameraView.releaseCamera();
}
}
#Override
protected void onPause() {
super.onPause();
// Do not hold the camera during onPause
if (cameraView != null) {
cameraView.releaseCamera();
}
}
Logcat Log for my application with Log Level "Info"
05-03 08:45:02.328 29785-29785/com.dhbw.charadect I/dalvikvm-heap﹕ Grow heap (frag case) to 5.955MB for 921616-byte allocation
05-03 08:45:03.305 29785-29785/com.dhbw.charadect I/Choreographer﹕ Skipped 51 frames! The application may be doing too much work on its main thread.
05-03 08:45:03.313 29785-29785/com.dhbw.charadect W/Resources﹕ Converting to boolean: TypedValue{t=0x3/d=0x210 "res/anim/decelerate_interpolator.xml" a=1 r=0x10a0006}
05-03 08:45:04.008 29785-29785/com.dhbw.charadect I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
05-03 08:45:14.414 29785-29797/com.dhbw.charadect I/Camera﹕ Received CAMERA_MSG_RELEASE
Thanks in advance for all answers and comments!

Android: No Activity found to handle Intent (trying to add an activity to exsisting app)

Ok so I am very new to the Android programming, I am starting week 2 of this class and cannot for the life of me figure out what is going wrong. I have read/watched tons of tutorials on adding new Activities and nothing works.
Assignment: Use the Activities app and add a fourth activity
My activity is simple, 3 buttons and an image. One button makes the image visible and the other makes it invisible. Third returns back to the main.
Note: I edited the original app to have buttons on Main Activity because it had me hitting center on the d-pad which I found dumb. Another note is that Activity 2 & 3 use the same layout and do basically the same thing from what I can tell
public class MainActivity extends Activity {
String tag = "Events";
int request_Code = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---hides the title bar---
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Log.d(tag, "In the onCreate() event");
Button act2Butt = (Button) findViewById(R.id.act2Butt);
Button act3Butt = (Button) findViewById(R.id.act3Butt);
Button act4Butt = (Button) findViewById(R.id.act4Butt);
act2Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
}
});
act3Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.ACTIVITY2"), request_Code);
}
});
act4Butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivityForResult(new Intent("net.learn2develop.MYACTIVITY"), request_Code);
}
});
}
/*
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
{
//startActivity(new Intent("net.learn2develop.ACTIVITY2"));
//startActivity(new Intent(this, Activity2.class));
startActivityForResult(new Intent(
"net.learn2develop.ACTIVITY2"),
request_Code);
}
return false;
}
*/
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == request_Code) {
if (resultCode == RESULT_OK) {
Toast.makeText(this,data.getData().toString(),
Toast.LENGTH_LONG).show();
}
}
}
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
public class MyActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity4);
Button yesButt = (Button) findViewById(R.id.yesButton);
Button noButt = (Button) findViewById(R.id.noButton);
Button finButt = (Button) findViewById(R.id.finButton);
final ImageView img1 = (ImageView) findViewById(R.id.image1);
yesButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
img1.setVisibility(View.VISIBLE);
}
});
noButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
img1.setVisibility(View.INVISIBLE);
}
});
finButt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();
data.setData(Uri.parse("OMG IT WORKS"));
setResult(RESULT_OK, data);
finish();
}
});
}
public class Activity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
String defaultName="";
Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString("Name");
}
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
txt_username.setHint(defaultName);
//---get the OK button---
Button btn = (Button) findViewById(R.id.btn_OK);
//---event handler for the OK button---
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) {
Intent data = new Intent();
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
//---set the data to pass back---
data.setData(Uri.parse(
txt_username.getText().toString()));
setResult(RESULT_OK, data);
//---closes the activity---
finish();
}
});
}
I have entered the code for Main Activity, My Activity (the one I made), and Activity 2. My Activity runs great and does exactly what I want it to but if I try to access it from main it dies.
928-928/net.learn2develop.Activities E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.learn2develop.Activities, PID: 928
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=net.learn2develop.MYACTIVITY }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3736)
at android.app.Activity.startActivityForResult(Activity.java:3697)
at net.learn2develop.Activities.MainActivity$3.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
This code is for my last attempt and making it work before throwing my hands up. Last thing I did was make My Activity act like the other and use startActivityForResult.
Any help would help greatly. I don't know if it matters or not but I do not have a .class for My Activity in the bin directory but there is one for all the others.
If you need any more info please just ask.
Like I said before I'm really new to the whole Android area.
Edit: Manifest
<activity android:name=".MyActivity"
android:label="My Activity">
<intent-filter>
<action android:name="net.learn2develop.MYACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You may need to add the Activity to the manifest. If you are sure you have done this, I would recommend using an Intent slightly differently.
Try using an Intent the following way:
Intent intent = new Intent(MyActivity.this, SecondActivity.class);
startActivityForResult(intent, requestCode);
The first parameter of this Intent is the current Activity. The second parameter is theActivity you wish to navigate to. Handling an Intent this way prevents a small typo in the package name which would throw that exception. As long as the Activity you are trying to navigate to is in the manifest and you have set up your Intent like the code above, everything should work fine.
Good luck and happy coding!
you need to add your activity to your manifest
<activity
android:name=".MySecondActivity"
android:label="#string/title_second_activity">
</activity>
net.learn2develop.MYACTIVITY
Android is not finiding the above activity like other engineers said,add this activity in your manifest file so JVM can find which class you are referring to.

Categories