gpt4 book ai didi

android - 在可绘制文件夹中共享 png 图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:29 24 4
gpt4 key购买 nike

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

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 放在 drawable 文件夹中。但是图片无法发送。那是因为在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();

}

}

最佳答案

我找到了不应该将图像复制到 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();
}

关于android - 在可绘制文件夹中共享 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18502598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com