How To Load An image from gallery






To load an image from gallery to an ImageView in Sketchware app, follow the steps given below.

1. In VIEW of your Sketchware project add a Button button1, and an ImageView imageview1.

2. Set an image as default image of imageview1.

3. In LOGIC area, in onCreate event, use add source directly block and put following code in it:
} private static int RESULT_LOAD_IMAGE = 1;
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); imageview1.setImageURI(selectedImage); }

The code above will set the selected image as image of imageview1.

If you want to set it as Background of imageview1, in place of code above, use following code.
} private static int RESULT_LOAD_IMAGE = 1;
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData();
try {
java.io.InputStream is = getContentResolver().openInputStream(selectedImage);
android.graphics.drawable.Drawable bg_img = android.graphics.drawable.Drawable.createFromStream(is, selectedImage.toString());
imageview1.setBackground(bg_img); }
catch (java.io.FileNotFoundException e) {
showMessage(e.toString());
} }

4. In on button1 Click event, use add source directly block and put following code in it:
Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE);


5. Save and Run the project. Now when you click button1, you can browse your gallery and select an image. The selected image will be displayed in imageview1.

2 Comments

Previous Post Next Post