无法附加位图图像。 Android的

时间:2012-08-05 06:52:54

标签: android android-intent uri share

我看到了有关此问题的其他问题,尝试了他们的答案,但是没有用。 我正在尝试使用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也会返回给我: /数据/数据/ mypackage的/文件/ 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
        }

1 个答案:

答案 0 :(得分:0)

请尝试使用 setData()代替 putExtra(),此处android developer site - intent set data

相关问题