简体   繁体   English

在android中显示大图像但共享较小的图像

[英]Show a large image but share a smaller image in android

I am making an Android application, which shares images via social media.我正在制作一个 Android 应用程序,它通过社交媒体共享图像。 The images are in a grid layout , so when a user presses an image it opens another activity which will show that specific image(Which can now be shared).图像采用网格布局,因此当用户按下图像时,它会打开另一个活动,该活动将显示该特定图像(现在可以共享)。 The images are stored in the drawable folder.图像存储在drawable文件夹中。 I want the image I share to be different (smaller - 32px) than the one the user views in its separate activity which is bigger (172px) .我希望我分享的图像与用户在其单独活动中查看的图像不同(更小 - 32像素) ,后者更大(172 像素)

Here is the Activity in which you view the full image in:这是您在其中查看完整图像的活动:

public class FullImage extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        Intent i = getIntent();

        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.mThumbIds[position]);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        OutputStream output;

        ImageView image = (ImageView) findViewById(R.id.full_image_view);
        Bitmap bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();

        File dir = new File(Environment.getExternalStorageDirectory() + "/images");

        dir.mkdirs();

        File file = new File(dir, "images.png");

        try {
            verifyStoragePermissions(this);
            output = new FileOutputStream(file);

            bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
            output.flush();
            output.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        switch (item.getItemId()) {
            case R.id.action_share:
                Uri uri = Uri.fromFile(file);
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                shareIntent.setType("image/png");
                startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.action_share)));

            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return true;

    }

    // Storage Permissions
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }
}

You can scale the bitmap before store it, or store a second reduced image.您可以在存储之前缩放位图,或存储第二个缩小的图像。

int PIXELS = 32;
...
bitmap = bitmap.createScaledBitmap(bitmap, PIXELS , PIXELS , true));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM