1
我正在制作一个Android应用程序,它通过社交媒体共享图像。图像在网格布局,所以当用户按下图像时,它会打开另一个活动,它将显示特定图像(现在可以共享)。图像存储在可绘制的文件夹中。我希望我分享的图像与用户在其单独的活动中观看的图像分享(较小-32像素)不一样,它是较大的(172像素)。显示一个大图像,但在android中共享一个较小的图像
这里是您在其中查看完整图片的活动:
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
);
}
}
}
使用Bitmap.createScaledBitmap从原始创建不同大小的位图:https://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap,INT ,int,boolean) –
您可以从主图像中提取缩略图,该缩略图的大小和分辨率会更低 –