如何使用“共享图像使用”共享意图在Android共享图像?

我有该应用程序中的图像厨房应用程序,我把所有的图像放入drawable-hdpi文件夹。 我像这样在我的活动中调用图像:

private Integer[] imageIDs = { R.drawable.wall1, R.drawable.wall2, R.drawable.wall3, R.drawable.wall4, R.drawable.wall5, R.drawable.wall6, R.drawable.wall7, R.drawable.wall8, R.drawable.wall9, R.drawable.wall10 }; 

所以,现在我想知道如何使用共享意向我分享这个图像我提出这样的共享代码:

  Button shareButton = (Button) findViewById(R.id.share_button); shareButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent sharingIntent = new Intent(Intent.ACTION_SEND); Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + imageIDs); sharingIntent.setType("image/jpeg"); sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); startActivity(Intent.createChooser(sharingIntent, "Share image using")); } }); 

我也有共享button,当我点击共享button共享框正在打开但是,当我cliked任何服务主要是它的崩溃或一些服务说:无法打开图像所以我如何解决这个问题或是否有任何其他格式的代码来分享图像????

编辑:

我尝试使用下面的代码。 但它不工作。

 Button shareButton = (Button) findViewById(R.id.share_button); shareButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent sharingIntent = new Intent(Intent.ACTION_SEND); Uri screenshotUri = Uri.parse("android.resource://com.android.test/*"); try { InputStream stream = getContentResolver().openInputStream(screenshotUri); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } sharingIntent.setType("image/jpeg"); sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri); startActivity(Intent.createChooser(sharingIntent, "Share image using")); } }); 

如果不介意有人纠正我的上面的代码或给我一个正确的例子PLZ如何从drawable-hdpi文件夹共享我的图像

 Bitmap icon = mBitmap; Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/jpeg"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes); File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"); try { f.createNewFile(); FileOutputStream fo = new FileOutputStream(f); fo.write(bytes.toByteArray()); } catch (IOException e) { e.printStackTrace(); } share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg")); startActivity(Intent.createChooser(share, "Share Image")); 

从superM提出的解决scheme为我工作了很长时间,但最近我在4.2(HTC One)上testing它,并停止在那里工作。 我知道这是一个解决方法,但它是唯一一个为我工作的所有设备和版本。

根据文档,开发人员被要求“使用系统MediaStore”来发送二进制内容。 然而,这具有(分散)优势,即媒体内容将被永久保存在设备上。

如果这是您的选项,则可能需要授予WRITE_EXTERNAL_STORAGE权限并使用系统范围的MediaStore。

 Bitmap icon = mBitmap; Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/jpeg"); ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, "title"); values.put(Images.Media.MIME_TYPE, "image/jpeg"); Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); OutputStream outstream; try { outstream = getContentResolver().openOutputStream(uri); icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream); outstream.close(); } catch (Exception e) { System.err.println(e.toString()); } share.putExtra(Intent.EXTRA_STREAM, uri); startActivity(Intent.createChooser(share, "Share Image")); 

首先添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用位图从res

 Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage); Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/jpeg"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null); Uri imageUri = Uri.parse(path); share.putExtra(Intent.EXTRA_STREAM, imageUri); startActivity(Intent.createChooser(share, "Select")); 

通过蓝牙和其他使者testing

奇迹般有效。

我发现最简单的方法是使用MediaStore临时存储您想要共享的图像:

 Drawable mDrawable = mImageView.getDrawable(); Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap(); String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image Description", null); Uri uri = Uri.parse(path); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/jpeg"); intent.putExtra(Intent.EXTRA_STREAM, uri); startActivity(Intent.createChooser(intent, "Share Image")); 

来自: 与意图共享内容

这是一个为我工作的解决scheme。 一个问题是你需要将图像存储在共享或非应用程序私有位置( http://developer.android.com/guide/topics/data/data-storage.html#InternalCache

许多build议表示存储在Apps “私人”caching位置,但这当然不能通过其他外部应用程序访问,包括正在使用的通用共享文件意图。 当你尝试这个,它会运行,但例如Dropbox会告诉你,该文件不再可用。

/ *步骤1 – 使用下面的文件保存function在本地保存位图文件。 * /

 localAbsoluteFilePath = saveImageLocally(bitmapImage); 

/ *步骤2 – 将非专用绝对文件path共享到共享文件意图* /

 if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") { Intent shareIntent = new Intent(Intent.ACTION_SEND); Uri phototUri = Uri.parse(localAbsoluteFilePath); File file = new File(phototUri.getPath()); Log.d(TAG, "file path: " +file.getPath()); if(file.exists()) { // file create success } else { // file create fail } shareIntent.setData(phototUri); shareIntent.setType("image/png"); shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION); } 

/ *保存图像function* /

  private String saveImageLocally(Bitmap _bitmap) { File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS); File outputFile = null; try { outputFile = File.createTempFile("tmp", ".png", outputDir); } catch (IOException e1) { // handle exception } try { FileOutputStream out = new FileOutputStream(outputFile); _bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); out.close(); } catch (Exception e) { // handle exception } return outputFile.getAbsolutePath(); } 

/ *第3步 – 处理共享文件意图结果。 需要远程临时文件等* /

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intents if (requestCode== Navigator.REQUEST_SHARE_ACTION) { // delete temp file File file = new File (localAbsoluteFilePath); file.delete(); Toaster toast = new Toaster(activity); toast.popBurntToast("Successfully shared"); } } 

我希望能帮助别人。

如何在android中共享图像progamatically,有时你想要快照你的观点,然后喜欢分享

所以请按照下列步骤操作:1.为mainfest文件添加权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

2.Very首先截取你的观点,比如说

Imageview,Textview,Framelayout,LinearLayout等

例如,你有一个图像视图来截图

在oncreate()中调用这个方法

  ImageView ivCircle = (ImageView)findViewById(R.id.iv_answer_circle); ///take a creenshot screenShot(ivCircle); 

在屏幕截图之后调用共享图像方法在button上

点击或者你想要的地方

 shareBitmap(screenShot(ivCircle),"myimage"); 

创build方法之后定义这两个方法

  public Bitmap screenShot(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } //////// this method share your image private void shareBitmap (Bitmap bitmap,String fileName) { try { File file = new File(getContext().getCacheDir(), fileName + ".png"); FileOutputStream fOut = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); file.setReadable(true, false); 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); } catch (Exception e) { e.printStackTrace(); } } 

简单和最简单的代码,您可以使用它来分享图库的图像。

  String image_path; File file = new File(image_path); Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_SEND); intent .setType("image/*"); intent .putExtra(Intent.EXTRA_STREAM, uri); context.startActivity(intent ); 

我在search不同的选项,从我的应用程序共享视图或图像到其他应用程序累了。 最后我得到了解决scheme。

步骤1: 共享意图处理块。 这将popup您的窗口与您的手机中的应用程序列表

 public void share_bitMap_to_Apps() { Intent i = new Intent(Intent.ACTION_SEND); i.setType("image/*"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); /*compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] bytes = stream.toByteArray();*/ i.putExtra(Intent.EXTRA_STREAM, getImageUri(mContext, getBitmapFromView(relative_me_other))); try { startActivity(Intent.createChooser(i, "My Profile ...")); } catch (android.content.ActivityNotFoundException ex) { ex.printStackTrace(); } } 

第2步: 将您的视图转换为BItmap

 public static Bitmap getBitmapFromView(View view) { //Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); //Get the view's background 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); // draw the view on the canvas view.draw(canvas); //return the bitmap return returnedBitmap; } 

第3步:

从位图图像获取URI

 public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } 

ref: – http://developer.android.com/training/sharing/send.html#send-multiple-content

 ArrayList<Uri> imageUris = new ArrayList<Uri>(); imageUris.add(imageUri1); // Add your image URIs here imageUris.add(imageUri2); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "Share images to..")); 

我只是有同样的问题。
这是一个答案,不要在你的主代码中使用任何明确的文件(让api帮你处理)。

 Drawable mDrawable = myImageView1.getDrawable(); Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap(); String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null); Uri uri = Uri.parse(path); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "Share Image")); 

这是path…你只需要添加你的图像ID在一个可绘制的对象。 在我的情况(上面的代码),drawable是从一个ImageView中提取的。

尝试这个,

 Uri imageUri = Uri.parse("android.resource://your.package/drawable/fileName"); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/png"); intent.putExtra(Intent.EXTRA_STREAM, imageUri); startActivity(Intent.createChooser(intent , "Share")); 

SuperM的答案为我工作,但与Uri.fromFile()而不是Uri.parse()。

使用Uri.parse(),它只能用于WhatsApp。

这是我的代码:

 sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFile)); 

Uri.parse()的输出:
/storage/emulated/0/Android/data/application_package/Files/17072015_0927.jpg

Uri.fromFile的输出:
文件:///storage/emulated/0/Android/data/application_package/Files/17072015_0927.jpg

 Strring temp="facebook",temp="whatsapp",temp="instagram",temp="googleplus",temp="share"; if(temp.equals("facebook")) { Intent intent = getPackageManager().getLaunchIntentForPackage("com.facebook.katana"); if (intent != null) { Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("image/png"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + "/sdcard/folder name/abc.png")); shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.setPackage("com.facebook.katana"); startActivity(shareIntent); } else { Toast.makeText(MainActivity.this, "Facebook require..!!", Toast.LENGTH_SHORT).show(); } } if(temp.equals("whatsapp")) { try { File filePath = new File("/sdcard/folder name/abc.png"); final ComponentName name = new ComponentName("com.whatsapp", "com.whatsapp.ContactPicker"); Intent oShareIntent = new Intent(); oShareIntent.setComponent(name); oShareIntent.setType("text/plain"); oShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Website : www.google.com"); oShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filePath)); oShareIntent.setType("image/jpeg"); oShareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); MainActivity.this.startActivity(oShareIntent); } catch (Exception e) { Toast.makeText(MainActivity.this, "WhatsApp require..!!", Toast.LENGTH_SHORT).show(); } } if(temp.equals("instagram")) { Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android"); if (intent != null) { File filePath =new File("/sdcard/folder name/"abc.png"); Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("image"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + "/sdcard/Chitranagari/abc.png")); shareIntent.setPackage("com.instagram.android"); startActivity(shareIntent); } else { Toast.makeText(MainActivity.this, "Instagram require..!!", Toast.LENGTH_SHORT).show(); } } if(temp.equals("googleplus")) { try { Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); String strDate = sdf.format(c.getTime()); Intent shareIntent = ShareCompat.IntentBuilder.from(MainActivity.this).getIntent(); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, "Website : www.google.com"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + "/sdcard/folder name/abc.png")); shareIntent.setPackage("com.google.android.apps.plus"); shareIntent.setAction(Intent.ACTION_SEND); startActivity(shareIntent); }catch (Exception e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "Googleplus require..!!", Toast.LENGTH_SHORT).show(); } } if(temp.equals("share")) { File filePath =new File("/sdcard/folder name/abc.png"); //optional //internal storage Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_TEXT, "Website : www.google.com"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(filePath)); //optional//use this when you want to send an image shareIntent.setType("image/jpeg"); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(Intent.createChooser(shareIntent, "send")); }