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

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();
}
}

Related

Crop Image Activity-Android Studio

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!"

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.

Return URI to other class

So I want this class, which chooses images out of the gallery and saves the data in the URI to pass this to my imageadapter.class and then display it on the Gridview. How can I do this ?
Thanks,
Code:
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.addimage:
Toast.makeText(getApplicationContext(), "Image added", Toast.LENGTH_SHORT).show();
openGallery();
break;
case R.id.loggout:
Toast.makeText(getApplicationContext(), "logged out", Toast.LENGTH_SHORT).show();
Intent i = new Intent(nav.this, MainActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
private void openGallery(){
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, IMAGE_PICK);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == IMAGE_PICK){
imageuri = data.getData();
System.out.println("success");
}
}
and the adapter's array of images:
public static Integer[] images ={
R.drawable.test, R.drawable.test2, R.drawable.test3, R.drawable.test4, R.drawable.test5, R.drawable.test6, R.drawable.test7, R.drawable.test8
};
If you are asking about sending the result you can do something like so upon successful retrieval of the data,
if(resultCode == RESULT_OK && requestCode == IMAGE_PICK){
Uri imageuri = data.getData();
processImageData(imageuri);
System.out.println("success");
}
Create a setter method at your adapter like
public void setImages(Integer[] images){
this.images = images;
notifyDataSetChanged();
}
and in onActivityResult() method however method you handle uri with create an array of images and call your adapter variable + setter
if(resultCode == RESULT_OK && requestCode == IMAGE_PICK){
//handling and create array list of uri
Integer[] imagesArrayResult = {//......//}
imageadapter.setImages(imagesArrayResult);
}

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 pass default image to previous activity if no image is selected

In my app, I have a camera function. User can capture image or pick from gallery.
Activity B
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}
}
}
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
if(selectedImage!=null) {
returnIntent.putExtra("img_uri", selectedImage.toString());
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
When submit button is clicked, it will return to previous activity.
Activity A
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
imgURI = Uri.parse(data.getStringExtra("img_uri"));
if (mClickedPosition == -1) {
if (obj != null)
obj.addNewItem(imgURI);
} else {
if (obj != null)
obj.changeItem(mClickedPosition, imgURI);
}
}
}
}
My question now is how to pass a default image to Activity A If
user didn't select any image ?
This is the xml imageView in Activity B
<ImageView
android:paddingTop="30dp"
android:layout_gravity="center"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#mipmap/no_image"
android:scaleType="fitXY"
android:id="#+id/imageView"
android:layout_weight="0.50" />
If no image selected, I want #mipmap/no_image return to Activity B. Is it possible ? Thanks.
You can pass an Uri of your image stored in resources by the next way:
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
if(selectedImage!=null) {
returnIntent.putExtra("img_uri", selectedImage.toString());
} else {
returnIntent.putExtra("img_uri", Uri.parse("android.resource://your.package.name/mipmap/no_image").toString());
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
Then you can use it as usual Uri to show the image in ImageView.
ByDefault set value of selectedImage to "default".
Now in your addNewItem() and changeItem() check value passed in imageUri
if(imageUri.equalsIgnoreCase("default"))
// set your default image that is #mipmap/no_image
else
// set image you are getting from imageUri
Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}else{
//set default
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}else{
//set default
}
default{
//set default
}
}
You don't need to return a default image to Activity A. Like others wrote, you just have to prepare for the condition when the user doesn't select any image.
Modify your onActivityResult for Activity A like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
}
}
break;
default:
//Your default image goes here, when the user doesn't select either of the two options
imageView.setImageResource(R.drawable.<default_image>);
break;
}
}

Categories