如何从图库中选取缩略图(android)

时间:2019-02-08 12:21:52

标签: thumbnails

如何从图库中选择缩略图?我需要一个小的图像,因为我必须将其存储在sqlite db中。在将大图像保存到数据库中之前,我尝试将大图像更改为缩略图,但是它仍然无法读取存储图像的列。

公共类MainActivity扩展了AppCompatActivity {

final static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonLoadImage = (Button) findViewById(R.id.button1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent gallery = new Intent(Intent.ACTION_PICK, Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(gallery, RESULT_LOAD_IMAGE);
        }
    });
}

//我如何尝试更改图片的大小:

@Override     受保护的void onActivityResult(int reqCode,int resultCode,Intent数据){         super.onActivityResult(reqCode,resultCode,data);

    if (resultCode == RESULT_OK) {
        try {
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            selectedImage = BitmapFactory.decodeStream(imageStream);
            final int THUMBNAIL_HEIGHT = 200;
            final int THUMBNAIL_WIDTH = 200;
            Float width  = new Float(selectedImage.getWidth());
            Float height = new Float(selectedImage.getHeight());
            Float ratio = width/height;
            selectedImage = Bitmap.createScaledBitmap(selectedImage, (int)(THUMBNAIL_HEIGHT*ratio), THUMBNAIL_HEIGHT, false);

            int padding = (THUMBNAIL_WIDTH - selectedImage.getWidth())/2;
            binding.imgView.setPadding(padding, padding, padding, padding);
            binding.imgView.setBackgroundColor(0);
            binding.imgView.setImageBitmap(selectedImage);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(AddRecipe.this, "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }else {
        Toast.makeText(AddRecipe .this, "You haven't picked Image",Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案