如何分享ImageView中的图片?

3
我是一个有用的助手,可以为您翻译文本。
我有一个ImageView,想分享它的图片。
以下是我的代码:
btshare.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                 View content = findViewById(R.id.full_image_view);
                 content.setDrawingCacheEnabled(true);
                     Bitmap bitmap = content.getDrawingCache();
                     File root = Environment.getExternalStorageDirectory();
                     File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
                     try 
                     {
                         root.createNewFile();
                         FileOutputStream ostream = new FileOutputStream(root);
                         bitmap.compress(CompressFormat.JPEG, 100, ostream);
                         ostream.close();
                     } 
                     catch (Exception e) 
                     {
                         e.printStackTrace();
                     }


                 Intent shareIntent = new Intent(Intent.ACTION_SEND);
                 Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
                 shareIntent.setData(phototUri);
                 shareIntent.setType("image/*");
                 shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
                 startActivity(Intent.createChooser(shareIntent, "Share Via"));

             }  



    });

当我按下按钮时,我会收到这些错误?
01-13 06:00:19.282: W/System.err(6199): java.io.FileNotFoundException: /storage/emulated/0: open failed: EISDIR (Is a directory)
01-13 06:00:19.286: W/System.err(6199):     at libcore.io.IoBridge.open(IoBridge.java:409)
01-13 06:00:19.286: W/System.err(6199):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-13 06:00:19.294: W/System.err(6199):     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-13 06:00:19.294: W/System.err(6199):     at com.safshari.mandegar.FullImageActivity$3.onClick(FullImageActivity.java:116)
01-13 06:00:19.294: W/System.err(6199):     at android.view.View.performClick(View.java:4240)
01-13 06:00:19.294: W/System.err(6199):     at android.view.View$PerformClick.run(View.java:17721)
01-13 06:00:19.298: W/System.err(6199):     at android.os.Handler.handleCallback(Handler.java:730)
01-13 06:00:19.298: W/System.err(6199):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-13 06:00:19.298: W/System.err(6199):     at android.os.Looper.loop(Looper.java:137)
01-13 06:00:19.302: W/System.err(6199):     at android.app.ActivityThread.main(ActivityThread.java:5103)
01-13 06:00:19.302: W/System.err(6199):     at java.lang.reflect.Method.invokeNative(Native Method)
01-13 06:00:19.302: W/System.err(6199):     at java.lang.reflect.Method.invoke(Method.java:525)
01-13 06:00:19.302: W/System.err(6199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-13 06:00:19.306: W/System.err(6199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-13 06:00:19.306: W/System.err(6199):     at dalvik.system.NativeStart.main(Native Method)
01-13 06:00:19.310: W/System.err(6199): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
01-13 06:00:19.310: W/System.err(6199):     at libcore.io.Posix.open(Native Method)
01-13 06:00:19.310: W/System.err(6199):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-13 06:00:19.310: W/System.err(6199):     at libcore.io.IoBridge.open(IoBridge.java:393)
01-13 06:00:19.314: W/System.err(6199):     ... 14 more
01-13 06:00:19.618: E/Genymotion(489): Could not open '/sys/class/power_supply/genymotion_fake_path/present'

我应该做什么,我的程序需要哪些权限?我已经声明了以下权限,但还需要哪些呢?
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6个回答

7

最终代码,任何人都可以使用它来保存和分享ImageView中的图像:

View content = findViewById(R.id.full_image_view);
                 content.setDrawingCacheEnabled(true);

                     Bitmap bitmap = content.getDrawingCache();
                     File root = Environment.getExternalStorageDirectory();
                     File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
                     try {
                         cachePath.createNewFile();
                         FileOutputStream ostream = new FileOutputStream(cachePath);
                         bitmap.compress(CompressFormat.JPEG, 100, ostream);
                         ostream.close();
                     } catch (Exception e) {
                         e.printStackTrace();
                     }


                     Intent share = new Intent(Intent.ACTION_SEND);
                     share.setType("image/*");
                     share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
                     startActivity(Intent.createChooser(share,"Share via"));

             }  

愉快地编码


我能否从图像视图分享而不是从文件/ URL分享? - bashan
3
谢谢,但是无效。我收到了一个错误信息:java.io.IOException: No such file or directory. - bashan

5

尝试使用以下代码分享您的图片:

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"));
    startActivity(Intent.createChooser(share,"Share via"));

请将这些权限添加到AndroidMenifest.xml中。
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

无法使用这三个权限和顶部的代码。通过蓝牙没有任何反应,通过其他程序则显示文件无效。 - Soheyl
同时,我在这一行上收到了一个警告,提示cachepath从未被使用。文件缓存路径cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); - Soheyl
01-13 10:32:44.381: D/AbsListView(12194): 调用unregisterIRListener() 01-13 10:32:45.066: D/AbsListView(12194): onDetachedFromWindow 01-13 10:32:45.066: D/AbsListView(12194): 调用unregisterIRListener() @zanky - Soheyl
这段代码对我来说是有效的。你的图像真的保存在设备中吗? - Zankhna
代码 content.setDrawingCacheEnabled(true); Bitmap bitmap = content.getDrawingCache(); File root = Environment.getExternalStorageDirectory(); File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); try { cachePath.createNewFile(); FileOutputStream ostream = new FileOutputStream(cachePath); bitmap.compress(CompressFormat.JPEG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } }代码 - Soheyl
显示剩余5条评论

2
您需要添加此权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

2

尝试先创建缓存来存储此图像,然后共享它,因为您只能共享对其他应用程序公开的图像。您无法共享对您的应用程序私有的内容。

使用Context.getExternalCacheDir()创建缓存,然后共享此缓存的内容。


0
Bundle intent = getIntent().getExtras();
cardView = (CardView) findViewById(R.id.card);
final String query = intent.getString("Query1");

db = new DataBaseHelper(Image.this);

Cursor c = db.getData(query);

if (c.getCount() != 0) {
    c.moveToFirst();

    do {
        image = c.getString(5);
        title=c.getString(3);
    } while (c.moveToNext());
}

txt.setText(title);

img.setImageDrawable(getResources()
    .getDrawable(getResources().getIdentifier(image, "drawable", getPackageName())));

如何通过Intent分享图片 - Be Coool

0
  • 从ImageView获取位图
imageview.buildDrawingCache();
Bitmap image = mainimg.getDrawingCache();
  • 转换URL
public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}
  • 然后是分享按钮
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, getImageUri(this,getImageUri));
startActivity(Intent.createChooser(share,"Share via"));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接