2

我看到了有关此的其他问题,尝试了他们的答案,但没有用。我正在尝试使用developer.android.com的第二种方法。(第二个白色子弹)。我使用适当的权限将图像保存到应用程序内部存储器。图像(位图)已成功保存,但我无法将其附加到新意图以共享它。这是我的代码:

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("*/*");//Attach all types (images/text)

        FileOutputStream fos;

        try{
        fos = openFileOutput(myfilename, Context.MODE_WORLD_READABLE);
        mybitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.flush();
//Image is stored successfully (checked data/data/mypackage/files/myfilename)

           Uri uri = Uri.fromFile(getFileStreamPath(myfilename));
           shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    }
    catch (Exception e){
        // noth
        //Log.e(TAG, e.getStackTrace().toString());
    }

       shareIntent.putExtra(Intent.EXTRA_TEXT, "some text also");

        return shareIntent;
    }

应用程序说不能附加媒体项目。Gmail 似乎附加了项目,但在发送邮件时它什么也没显示!

uri.getPath 也返回我:/data/data/mypackage/files/myfilename,这是正确的

有任何想法吗?谢谢!

编辑:修改代码以使用 sd 卡。仍然无法正常工作:

        File imgDir;
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED))
imgDir = new File(
                android.os.Environment.getExternalStorageDirectory(),
                ".MyAppName/Images");
        else imgDir = MyActivity.this.getCacheDir();
        if (!imgDir.exists()) imgDir.mkdirs();

        File output = new File(imgDir, myFilename);

        OutputStream imageFileOS;
        try{
            Uri uriSavedImage = Uri.fromFile(output);
            imageFileOS = getContentResolver().openOutputStream(
                    uriSavedImage);
            bitmapBookCover.compress(Bitmap.CompressFormat.PNG, 90,
                    imageFileOS);


            imageFileOS.flush();
            imageFileOS.close();
//Again image successfully saved

            shareIntent.putExtra(Intent.EXTRA_STREAM, uriSavedImage);
        }
        catch (Exception e){
            // noth
        }
4

1 回答 1

0

请尝试使用setData()而不是putExtra(),这里是 android 开发者网站 - 意图集数据

于 2012-08-05T07:38:06.023 回答