This is my code and when i run the code then
output will be "**you haven't picked
images"**
sdk
which i use currently in this
app
android {
compileSdkVersion 28
defaultConfig {
applicationId
"com.example.letsget.humanitysavior"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName
"1.0"
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
}
buildTypes
{
release {
minifyEnabled false
proguardFiles
getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Here is
the manifests
permissions
android:name="android.permission.INTERNET" />
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
android:name="android.permission.RECORD_AUDIO"
/>
Here
is the fragment code
public class NewPostPublicFragment
extends Fragment {
public
NewPostPublicFragment() {
// Required empty public
constructor
}
private int PICK_IMAGE_MULTIPLE =
3;
String imageEncoded;
List
imagesEncodedList;
private GridView gvGallery;
private
NewPostImagesAdapter galleryAdapter;
CarouselView
carouselView;
private ArrayList
mArrayUri;
@Override
public View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view =
inflater.inflate(R.layout.fragment_new_post_public, container,
false);
gvGallery =
view.findViewById(R.id.gridview);
ImageView selectimgicon =
view.findViewById(R.id.selectimgicon);
ImageView selectvideoicon =
view.findViewById(R.id.selectvideoicon);
ImageView selectcameraicon =
view.findViewById(R.id.selectcameraicon);
carouselView =
view.findViewById(R.id.carouselview);
final TextInputEditText postcategories
= view.findViewById(R.id.postcategories);
//carouselView.setPageCount(mThumbIds.length);
//carouselView.setImageListener(imageListener);
//for
image selection
selectimgicon.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
//LOGIC FOR
PROFILE PICTURE
selectImagesFromGallery();
}
});
postcategories.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v)
{
final CharSequence[] items = { "Book", "Cloth", "Electronic",
"Furniture", "Bag", "Social Post", "Other" };
AlertDialog.Builder
postCategoryBuilder = new
AlertDialog.Builder(v.getContext(),AlertDialog.THEME_HOLO_LIGHT);
postCategoryBuilder.setTitle("Select Post Category");
postCategoryBuilder.setIcon(R.mipmap.categoryicon);
postCategoryBuilder.setItems(items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which) {
// the user clicked on colors[which]
postcategories.setText(items[which]);
}
});
postCategoryBuilder.show();
}
});
return view;
}
ImageListener imageListener = new
ImageListener() {
@Override
public void setImageForPosition(int
position, ImageView imageView) {
//imageView.setImageResource(mThumbIds[position]);
imageView.setImageURI(mArrayUri.get(position));
}
};
private void selectImagesFromGallery()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,
true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"),
PICK_IMAGE_MULTIPLE);
}
@Override
public
void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try
{
Log.v(TAG, "requestCode=" + requestCode + ", resultCode = "+
resultCode + ", data = " + data);
// When an Image is picked
if
(requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && data
!= null) {
// Get the Image from data
String[] filePathColumn = { MediaStore.Images.Media.DATA };
imagesEncodedList
= new ArrayList();
if(data.getData()!=null){
Uri mImageUri =
data.getData();
// Get the cursor
Cursor cursor =
getActivity().getContentResolver().query(mImageUri,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex =
cursor.getColumnIndex(filePathColumn[0]);
imageEncoded =
cursor.getString(columnIndex);
cursor.close();
ArrayList mArrayUri = new ArrayList();
mArrayUri.add(mImageUri);
galleryAdapter = new
NewPostImagesAdapter(getActivity().getApplicationContext(),mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0,
0);
carouselView.setPageCount(mArrayUri.size());
carouselView.setImageListener(imageListener);
} else
{
if (data.getClipData() != null) {
ClipData mClipData =
data.getClipData();
ArrayList mArrayUri = new
ArrayList();
for (int i = 0; i < mClipData.getItemCount(); i++)
{
ClipData.Item item = mClipData.getItemAt(i);
Uri uri
= item.getUri();
mArrayUri.add(uri);
// Get the
cursor
Cursor cursor =
getActivity().getContentResolver().query(uri, filePathColumn, null, null,
null);
// Move to first row
cursor.moveToFirst();
int columnIndex =
cursor.getColumnIndex(filePathColumn[0]);
imageEncoded =
cursor.getString(columnIndex);
imagesEncodedList.add(imageEncoded);
cursor.close();
galleryAdapter = new
NewPostImagesAdapter(getActivity().getApplicationContext(),mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0,
0);
}
Log.v("LOG_TAG", "Selected Images" +
mArrayUri.size());
}
}
} else {
Toast.makeText(getContext(), "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
when
i run this app and tap on image button then it open the gallery and select the images
after that when i press the done button then it's not display images it just display
toast message you haven't picked images
No comments:
Post a Comment