我尝试使用https://graph.microsoft.com
中的代码共享图像,但是在下面出现了很多错误:
ViewPictureActivity.java
public class ViewPictureActivity extends AppCompatActivity {
private static final String KEY_PICTURES = "pics";
private static final String KEY_REQUESTED_POS = "requestedPos";
private ArrayList<Integer> imageIDs;
private int currentPosition = -1;
private ImageView galleryPicture;
Bitmap bitmap;
private File imagePath;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_picture);
initToolbar();
galleryPicture = (ImageView) findViewById(R.id.galleryImage);
imageIDs = (ArrayList<Integer>) getIntent().getSerializableExtra(KEY_PICTURES);
currentPosition = getIntent().getIntExtra(KEY_REQUESTED_POS, 0);
changePicture(currentPosition);
final ImageButton previousItemButton = findViewById(R.id.previous_item_button);
previousItemButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v1) {
if (imageIDs != null && imageIDs.size() > 0 && currentPosition > 0) {
changePicture(--currentPosition);
}
}
});
final ImageButton nextItemButton = findViewById(R.id.next_item_button);
nextItemButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v1) {
if (imageIDs != null && imageIDs.size() > 0 && imageIDs.size() > currentPosition + 1) {
changePicture(++currentPosition);
} else {
Toast.makeText(getApplicationContext(), "No more pictures", Toast.LENGTH_SHORT).show();
}
}
});
final ImageButton btnshare = findViewById(R.id.btnShare);
btnshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
OutputStream output;
FrameLayout fm = (FrameLayout) findViewById(R.id.frm2);
Calendar cal = Calendar.getInstance();
Bitmap bitmap = Bitmap.createBitmap(fm.getWidth(),
fm.getHeight(), Config.ARGB_8888);
Canvas b = new Canvas(bitmap);
fm.draw(b);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()
+ "/D_Envitation/");
dir.mkdir();
String imagename = "image" + cal.getTimeInMillis() + ".png";
// Create a name for the saved image
File file = new File(dir, imagename);
// Show a toast message on successful save
Toast.makeText(ViewPictureActivity.this, "Image Saved to SD Card",
Toast.LENGTH_SHORT).show();
try {
output = new FileOutputStream(file);
// Compress into png format image from 0% - 100%
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Uri screenshotUri = Uri.fromFile(file);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(share, "Share With"));
}
});
}
private void initToolbar() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Toolbar displayActToolbar = findViewById(R.id.simple_toolbar);
setSupportActionBar(displayActToolbar);
}
}
private void changePicture(int arrayPosition) {
//galleryPicture.setLayoutParams(new LinearLayout.LayoutParams(500,500));
//galleryPicture.setScaleType(ImageView.ScaleType.CENTER_CROP);
galleryPicture.setImageResource(imageIDs.get(arrayPosition));
}
// starter - to control passing and reading Extra in the same class
public static void start(Context context, ArrayList<Integer> imageIDs, int requestedPos) {
Intent starter = new Intent(context, ViewPictureActivity.class);
starter.putExtra(KEY_PICTURES, imageIDs);
starter.putExtra(KEY_REQUESTED_POS, requestedPos);
context.startActivity(starter);
//Log.d("ViewPictureActivity", "after start");
}
答案 0 :(得分:0)
您可以与其他应用共享图像,而无需保存到移动设备中
public void OnClickShare(View view){
Bitmap bitmap =getBitmapFromView(YourView);
try {
File file = new File(this.getExternalCacheDir(),"logicchip.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
//start share image with intent
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
以及从ImageView获取位图的方法
// can replace parmeter with ImageView in your state
private Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
//or use #getDrawable with ImageView
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
希望有帮助,不要忘记阅读代码中的注释以了解您想要的内容