12

我正在将共享与应用程序的以下代码集成。

private void socialShare()
    {
        Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon");
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp");
        shareIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(shareIntent, "Share from"));
    }

如上面的代码,我试图把png image它放在可绘制的文件夹中。但是图片无法发送。那是因为在 setType 中,它是image/jpeg吗?我不能使用 jpeg,因为它失去了透明度。有人可以建议我如何与图像分享吗?

这是我用来将图像从drawable复制到sdcard的代码:

String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder"; 
        File direct = new File(commonPath);

        if(!direct.exists())
        {
            if(direct.mkdir()) 
              {
                Log.d("tag","directory created");
               //directory is created;
              }

        }

        Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.sharingimage);
        OutputStream outStream = null;
           File savingFile = new File(commonPath, "shareImage.png");
           if(!savingFile.exists())
           {
               Log.d("tag","file is created");

           try {
                savingFile.createNewFile();
                outStream = new FileOutputStream(savingFile);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();

                Log.d("tag","Saved");

               } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

               }

            }
4

6 回答 6

18

我找到了不应该在 SD 卡上复制图像的解决方案。这里是:

    try {
        Uri imageUri = null;
        try {
            imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(),
                    BitmapFactory.decodeResource(getResources(), R.drawable.fragment_menu_logo), null, null));
        } catch (NullPointerException e) {
        }
        String text = getString(R.string.share_text);
        // Launch the Google+ share dialog with attribution to your app.
        Intent shareIntent = new PlusShare.Builder(mActivity)
                .setType("image/*")
                .setText(text)
                .addStream(imageUri)
                .getIntent();
        startActivity(shareIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(mActivity, mActivity.getString(R.string.share_google_plus_not_installed), Toast.LENGTH_LONG).show();
    }
于 2014-06-13T12:50:13.040 回答
11
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg";
OutputStream out = null;
File file=new File(path);
try {
    out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
Intent shareIntent = new Intent();
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/jpg");
startActivity(Intent.createChooser(shareIntent,"Share with"));

这对我来说非常有效,这需要写权限

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

将此行添加到 android manifest 以获得写入权限。

于 2015-02-19T12:08:13.610 回答
10

您可以使用以下方法分享...

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing");
startActivity(Intent.createChooser(shareIntent, "Send your image"));

久经考验...为我工作

于 2013-08-29T05:48:29.447 回答
6

首先添加权限

<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"));

通过蓝牙测试。

奇迹般有效。

于 2015-05-07T09:06:09.013 回答
1

您不能直接uri从您的应用程序内部存储中共享一个(当然您的应用程序的资源将始终在内部存储中)

有两种方法可以实现这一点..

  1. 将您的图像复制到外部存储,然后从那里共享。看到这个

  2. 编写一个内容提供程序来共享图像。为此,请参阅从内部存储创建和共享文件

于 2013-08-29T05:06:07.010 回答
1

无需许可。无需实现内容提供者。

这就是你需要的:

显现:

        <provider
            android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}"
            android:exported="false" android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>

res/xml/provider_paths.xml

<paths>
    <cache-path
        name="cache_root" path="." />
</paths>

以及使用其意图的代码:

@WorkerThread
fun shareDrawable(context: Context, @DrawableRes resourceId: Int, fileName: String): Intent {
    val bitmap = ResourcesCompat.getDrawable(context.resources, resourceId, null)!!.toBitmap()
    val outputFile = File(context.cacheDir, "$fileName.png")
    val outPutStream = FileOutputStream(outputFile)
    bitmap.compress(CompressFormat.PNG, 100, outPutStream)
    outPutStream.flush()
    outPutStream.close()
    val shareIntent = Intent(Intent.ACTION_SEND)
    shareIntent.putExtra(Intent.EXTRA_STREAM, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        androidx.core.content.FileProvider.getUriForFile(context, context.packageName, outputFile)
    else
        Uri.fromFile(outputFile))
    shareIntent.type = "image/png"
    return shareIntent
}
于 2022-01-20T17:44:26.977 回答