I am writing this mobile app, on main screen there is a grid view which should display all the photos on the mobile device. And a button to take photos.
I already wrote the imageAdaptor, and now my app could display photos I put in the R.drawable folder.
But can I put all photos stored on the device instead? Including all the photos taken by this app.
Thanks in advance.
Here are my codes:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="12dp" />
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"
/>
Main method:
public class MainActivity extends AppCompatActivity {
public final String APP_TAG = "PhotoApp";
public String photoFileName = "photo.jpg";
private Uri imageUri;
MarshMallowPermission marshMallowPermission = new MarshMallowPermission(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
};
public Uri getFileUri(String fileName){
Uri imageUri=null;
String typestr = "/images/";
// Get safe storage directory for photos
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getPath(), typestr+fileName);
// Create the storage directory if it does not exist
if (!mediaStorageDir.getParentFile().exists() && !mediaStorageDir.getParentFile().mkdirs()) {
Log.d(APP_TAG, "failed to create directory");
}
//creating a URI for the file we just created
imageUri=Uri.fromFile(mediaStorageDir);
return imageUri;
}
public void takePhoto(View v ){
// Check permissions
if (!marshMallowPermission.checkPermissionForCamera()
|| !marshMallowPermission.checkPermissionForExternalStorage()){
marshMallowPermission.requestPermissionForCamera();
} else {
// set file name, will pass to getFileUri to convert it into a URI
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
photoFileName = "IMG_"+timeStamp+".jpg";
Uri file_uri = getFileUri(photoFileName);
//just to print it to see if everything alright
System.out.println(file_uri);
//a intent specifically to start the android camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//a intent used in conjunction with the image/video taking, telling where (in URI formate ) where to store the picture taken
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
// Start the image capture intent to take photo
startActivityForResult(takePictureIntent, MY_PERMISSIONS_REQUEST_OPEN_CAMERA);
}
}
}
*/
Adaptor:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(250, 250));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.s1, R.drawable.s2,
R.drawable.s4, R.drawable.s5,
R.drawable.s6, R.drawable.s3,
};
}
Here's the solution btw, sorry It took so long to I wasn't aware that others were waiting for the answer. Sorry. The relevant code is below.
This is AsyncTask that will query the device's MediaStore and retrieve all the photos. Note that this is retrieving the thumbnails and not the full size images, and more specifically it's the MICRO_KIND. There is also a Thumbnail.MINI_KIND as well.
Here's the adapter for the GridView that's going to bind your thumbnails to the gridview. My issue was simply in the getView method of my ImageAdapter, notice I'm setting my ImageView resources from different indexes of an array of Bitmaps I called windows.