Это мой код, и когда я запускаю код, вывод будет "** вы не выбрали изображения " **
SDK, который я использую в настоящее время вэто приложение
android {compileSdkVersion 28 defaultConfig {applicationId "com.example.letsget.humanitysavior" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner }TunJunder.Rurelease {minifyEnabled false proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro'}}}
Вот разрешения для манифестов
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Вот фрагмент кода
открытый класс NewPostPublicFragment расширяет фрагмент {
public NewPostPublicFragment() {
// Required empty public constructor
}
private int PICK_IMAGE_MULTIPLE = 3;
String imageEncoded;
List<String> imagesEncodedList;
private GridView gvGallery;
private NewPostImagesAdapter galleryAdapter;
CarouselView carouselView;
private ArrayList<Uri> 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<String>();
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<Uri> mArrayUri = new ArrayList<Uri>();
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<Uri> mArrayUri = new ArrayList<Uri>();
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();
}
}
}
, когда я запускаю это приложение и нажимаю на изображениезатем откройте галерею и выберите изображения, после чего, когда я нажимаю кнопку «Готово», отображаются не изображения, а просто всплывающее сообщение о том, что вы не выбрали изображения