Crop Image Activity-Android Studio - java

protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE ){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK) {
imageUri = result.getUri();
image_added.setImageURI(imageUri);
}
} else {
Toast.makeText(this, "Something went wrong , try again!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(PostActivity.this , MainActivity2.class));
finish();
}
}
My code still works but when I select the image from gallery it does not get selected it directly gives the toast "Something went wrong , try again!"

Related

How to get image uri from camera intent?

i am trying to get the uri of the picture that have been taken from device camera. I just tried some of the advices from online but i could not manage to solve this problem here is the my code parts.
ImageView petImage;
Uri imageData;
petImage = view.findViewById(R.id.imgPetPic);
petImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// display error state to the user
Toast.makeText(getActivity(), "Camera is Not Available", Toast.LENGTH_SHORT).show();
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
imageData = data.getData();
Bitmap bp = (Bitmap) data.getExtras().get("data");
petImage.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED)
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
}
}
After that code parts worked. ımageData variable is still null. How do i fix this.

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I'm trying to create a login/register part of a project, and I'm having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.
Intent i = new Intent(this, SignUpActivity.class);
i.putExtra("username_info", username_info);
i.putExtra("password_info", password_info);
i.putExtra("name_info", name_info);
startActivityForResult(i, 101);
After values are added in the other activity, it's sent back like this (the arraylists have the same name in both activities):
Intent r = new Intent();
r.putExtra("username_info", username_info);
r.putExtra("password_info", password_info);
r.putExtra("name_info", name_info);
setResult(Activity.RESULT_OK, r);
finish();
}
and it's received here:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = getIntent().getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}
But the array lists are unchanged when I get back to the MainActivity. I'm new to Android Studio, so I might just be making a simple mistake.
EDIT:
I'm crashing when the username and password don't match, but it should be returning a toast instead:
#Override
public void onClick(View v) {
username = username_input.getText().toString();
password = password_input.getText().toString();
int index = username_info.indexOf(username);
if (username_info.size() < 1) {
Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
}
else if (password_info.get(index).equals(password)) {
Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
// open activity
}
else {
Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
}
}
});
Don't use getIntent() in onActivityResult, instead use the Intent data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = data.getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}

How to save image in internal storage?

I am selecting image and videos from gallery using intent.here is the code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/* video/*");
startActivityForResult(galleryIntent,RESULT_LOAD_IMG);
onActivityResult method:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
// VideoView videoView = (VideoView) findViewById(R.id.vidView);
// videoView.setVideoURI(selectedImage);
// videoView.start();
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
My question is how can I save this selected picture or video in internal storage and then play from SD card? I tried Google, but was unable to find anything. Kindly help.

How to adding image item to RecyclerView using a button without internet?

I am trying to use a adding button to get the photo from the gallery and put it to RecyclerView with text. When the user selected image form the gallery, the application will return the image path, but now, I no idea how to use the path to get the image and put it to RecyclerView to display with text, here is my coding:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
Toast.makeText(getActivity(), "Yor are picked image from" + selectedImage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You haven't picked image.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG)
.show();
}
}
Sorry for my English. Thank you for help.
Here is my adding button
In your adapter class you have to implement add method, something like this:
public void addImage(String imagePath) {
imagesPaths.add(imagePath);
notifyDataSetChanged();
}
notifyDataSetChanged() is crucial, because it calls for update on RecyclerView.
So in your onActivityResult you just have to pass image path to adapter:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
// Here you pass image path
yourAdapter.add(selectedImage.toString());
Toast.makeText(getActivity(), "Yor are picked image from" + selectedImage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You haven't picked image.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG)
.show();
}
}

Receiving result in android

I am beginner to android..I Integrated qr scanner in my app..problem is I added qr scanner button in two fragment..for receiving result I added on activityResult method..In my mainactivity..Is any another method for receiving result in android Instead of OnactivityResult method..If not their I should I add multiple onActivityResult method In my Mainactivty..can anyOne help me..
below is the my code for onActivityresult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode, resultCode,
data);
if (scanResult != null) {
if (scanResult.getContents() == null) {
Log.d("ScanFragment", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " " + scanResult.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
//result fragment
super.onActivityResult(requestCode, resultCode, data);
}
// }
}
static final int FRAGMENT_ONE_REQUEST = 1; // The request code
static final int FRAGMENT_TWO_REQUEST = 2; // The request code
then use this from fragment one
startActivityForResult(intent, FRAGMENT_ONE_REQUEST);
then use this from fragment two
startActivityForResult(intent, FRAGMENT_TWO_REQUEST);
in each fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
//some code
if (requestCode == FRAGMENT_ONE_REQUEST) {
}
}
}

Categories