If your Images are static and in drawable folder etc So send the bitmap from intent to next activity very costly( For Machine :) ) So Now is the best approach is to firstly create string-array
in strings.xml
like here.
<resources>
<string name="app_name">Gallery App</string>
<string-array name="images">
<item>@drawable/img1</item>
<item>@drawable/img2</item>
<item>@drawable/img3</item>
<item>@drawable/img4</item>
<item>@drawable/img5</item>
<item>@drawable/img6</item>
</string-array>
</resources>
Then in your activity to get List of images related to images
that array declared in above.
1:-Declare the instance of TypeArray typeArray
2:-Assign typeArray = getResources().obtainTypedArray(R.array.images);
3:-Declare your List and add each item one by one ->
ArrayList list = new ArrayList();
for (int i = 0 ;i< typedArray.length() ; i++){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),typedArray.getResourceId(i ,-1));
list.add(bitmap);
}
4:-Of course now you will set this list in your ArrayAdapter
view.setAdapter(new ArrayAdapter(this , R.layout.viewImages , list));
5:-Send cliked bitmapimage to your next Activity
view.setOnItemClickedListener(new AdapterView.OnItemClickedListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(MainActivity.this , DetailedImageActivity.class);
int imageId = typedArray.getResourceId(position , -1);
i.putExtra("imageId",imageId) ;
Log.i("ResourceId ","here");
startActivity(i);
}
});
6:-Recieve your bitmap Id and get Your bitmap Image
public class DetailedImageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_image);
ImageView imageView = (ImageView) findViewById(R.id.detailImageView);
String title = getIntent().getStringExtra("title");
int imageId = getIntent().getExtras().getInt("imageId") ;
Bitmap image = BitmapFactory.decodeResource(getResources() ,imageId);
imageView.setImageBitmap(image);
}
}