I'm using a library from github
to capture image or selecting images from the gallery on click of a button.
On clicking a button from one activity it opens a class from the library that allows us to capture an image or select an image from the gallery and display it in a Linearlayout.
I've modified a little to display it in a GridView. But the problem that i'm facing is that the GridView reads all the images that are already existing in a particular folder.
My goal to achieve is, if i capture an image or select an image from the gallery only those image must show up in the list
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int INTENT_REQUEST_GET_IMAGES = 13;
private static final int INTENT_REQUEST_GET_N_IMAGES = 14;
private Context mContext;
View getImages, getNImages;
private GridView mSelectedImagesContainer;
HashSet<Uri> mMedia = new HashSet<Uri>();
private List<String> listOfImagesPath;
public static final String GridView_ImagePath =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = MainActivity.this;
mSelectedImagesContainer = (GridView) findViewById(R.id.selected_photos_container);
getImages = findViewById(R.id.get_images);
getImages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getImages();
}
});
getNImages = findViewById(R.id.get_n_images);
getNImages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getNImages();
}
});
}
private void getImages() {
Intent intent = new Intent(mContext, ImagePickerActivity.class);
startActivityForResult(intent, INTENT_REQUEST_GET_IMAGES);
}
private void getNImages() {
Intent intent = new Intent(mContext, ImagePickerActivity.class);
Config config = new Config.Builder()
.setTabBackgroundColor(R.color.white)
.setTabSelectionIndicatorColor(R.color.blue)
.setCameraButtonColor(R.color.orange)
.setSelectionLimit(Integer.MAX_VALUE)
.build();
ImagePickerActivity.setConfig(config);
startActivityForResult(intent, INTENT_REQUEST_GET_N_IMAGES);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == INTENT_REQUEST_GET_IMAGES || requestCode == INTENT_REQUEST_GET_N_IMAGES) {
Parcelable[] parcelableUris = intent.getParcelableArrayExtra
(ImagePickerActivity.EXTRA_IMAGE_URIS);
if (parcelableUris == null) {
return;
}
Uri[] uris = new Uri[parcelableUris.length];
System.arraycopy(parcelableUris, 0, uris, 0, parcelableUris.length);
if (uris != null) {
for (Uri uri : uris) {
Log.i(TAG, " uri: " + uri);
mMedia.add(uri);
}
showMedia();
}
}
}
}
private void showMedia() {
Iterator<Uri> iterator = mMedia.iterator();
ImageInternalFetcher imageFetcher = new ImageInternalFetcher(this, 500);
while (iterator.hasNext()) {
Uri uri = iterator.next();
Log.i(TAG, " uri: " + uri);
if (mMedia.size() >= 1) {
mSelectedImagesContainer.setVisibility(View.VISIBLE);
}
View imageHolder = LayoutInflater.from(this).inflate(R.layout.media_layout, null);
ImageView thumbnail = (ImageView) imageHolder.findViewById(R.id.media_image);
if (!uri.toString().contains("content://")) {
uri = Uri.fromFile(new File(uri.toString()));
}
imageFetcher.loadImage(uri, thumbnail);
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if (listOfImagesPath != null) {
mSelectedImagesContainer.setAdapter(new ImageListAdapter(this, listOfImagesPath));
} else {
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
int wdpx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
getResources().getDisplayMetrics());
int htpx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
getResources().getDisplayMetrics());
thumbnail.setLayoutParams(new FrameLayout.LayoutParams(wdpx, htpx));
}
}
private List<String> RetriveCapturedImagePath() {
List<String> tFileList = new ArrayList<String>();
File f = new File(GridView_ImagePath);
if (f.exists()) {
File[] files=f.listFiles();
Arrays.sort(files);
for(int i=0; i<files.length; i++){
File file = files[i];
if(file.isDirectory())
continue;
tFileList.add(file.getPath());
}
}
return tFileList;
}
}
What I have tried:
I'm able to populate GridView with captured or selected image but the image that is loaded in the grid contains all the image that was already existing in that folder.
I kind of feel that since i'm specifying the image path as public static final String GridView_ImagePath =
Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/";
and in the function RetriveCapturedImagePath() it reads through all the images in that folder and attach all the path as the result, the gridview shows all the images.
So how can i modify it?