Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否共享和保存ImageView的图像?_Android_Share - Fatal编程技术网

Android 是否共享和保存ImageView的图像?

Android 是否共享和保存ImageView的图像?,android,share,Android,Share,我的应用程序有两个按钮,一个用于共享图像,另一个用于将其保存到内存 保存按钮工作正常,但共享按钮不工作 下面是保存按钮: public void exportImage () { if (this.mImageDrawableSet == false) { this.mSaveImageOnDisplay = true; return; } try { final Bitmap bitmap = getImageBitma

我的应用程序有两个按钮,一个用于共享图像,另一个用于将其保存到内存 保存按钮工作正常,但共享按钮不工作

下面是保存按钮:

public void exportImage () {
    if (this.mImageDrawableSet == false) {
        this.mSaveImageOnDisplay = true;
        return;
    }

    try {
        final Bitmap bitmap = getImageBitmap();
        if (bitmap == null) {
            Toast.makeText(getActivity(), "Something Went Wrong, Please Try Again!", Toast.LENGTH_SHORT).show();
            return;
        }

        final File dir = new File(Environment.getExternalStorageDirectory(), super.getResources().getString(R.string.config_external_storage_folder));

        if (!dir.exists()) {
            dir.mkdirs();
        }

        final File img = new File(dir, this.mNode.name + ".png");
        if (img.exists()) {
            img.delete();
        }

        final OutputStream outStream = new FileOutputStream(img);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();

        super.getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + dir.toString())));
        Toast.makeText(getActivity(), "Wallpaper Saved To, " + img.toString() + "!", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Log.e(TAG, "", e);
        Toast.makeText(getActivity(), "Something Went Wrong, Please Try Again!", Toast.LENGTH_SHORT).show();
    }
}
以下是分享部分:

public void shareimage () {
    if (this.mImageDrawableSet == false) {
        this.mApplyImageOnDisplay = true;
        return;
    }

    try {
        final Bitmap bitmap = getImageBitmap();
        if (bitmap == null) {
            Toast.makeText(getActivity(), "Something Went Wrong, Please Try Again!", Toast.LENGTH_SHORT).show();
            return;
        }       
        File root = Environment.getExternalStorageDirectory();
        File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");

            cachePath.createNewFile();
            FileOutputStream ostream = new FileOutputStream(cachePath);
            bitmap.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/*");
            share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
            startActivity(Intent.createChooser(share,"Share via"));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), "Something Went Wrong, Please Try Again!", Toast.LENGTH_SHORT).show();
        }
    }
logcat上什么也没显示,我只是搞错了。 权限是正确的

logcat

02-15 16:10:50.527: W/System.err(2033):     at      android.support.v4.app.Watson.onMenuItemSelected(Watson.java:126)
02-15 16:10:50.539: W/System.err(2033):     at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:604)
02-15 16:10:50.539: W/System.err(2033):     at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:92)
02-15 16:10:50.539: W/System.err(2033):     at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:204)
02-15 16:10:50.539: W/System.err(2033):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:986)
02-15 16:10:50.539: W/System.err(2033):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-15 16:10:50.539: W/System.err(2033):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
02-15 16:10:50.539: W/System.err(2033):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-15 16:10:50.539: W/System.err(2033):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
02-15 16:10:50.539: W/System.err(2033):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
02-15 16:10:50.539: W/System.err(2033):     at android.view.View.performClick(View.java:4240)
02-15 16:10:50.539: W/System.err(2033):     at android.view.View$PerformClick.run(View.java:17721)
02-15 16:10:50.543: W/System.err(2033):     at android.os.Handler.handleCallback(Handler.java:730)
02-15 16:10:50.543: W/System.err(2033):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 16:10:50.543: W/System.err(2033):     at android.os.Looper.loop(Looper.java:137)
02-15 16:10:50.543: W/System.err(2033):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-15 16:10:50.543: W/System.err(2033):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 16:10:50.543: W/System.err(2033):     at java.lang.reflect.Method.invoke(Method.java:525)
02-15 16:10:50.543: W/System.err(2033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-15 16:10:50.551: W/System.err(2033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-15 16:10:50.551: W/System.err(2033):     at dalvik.system.NativeStart.main(Native Method)
02-15 16:10:50.551: W/System.err(2033): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-15 16:10:50.555: W/System.err(2033):     at libcore.io.Posix.open(Native Method)
02-15 16:10:50.555: W/System.err(2033):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-15 16:10:50.555: W/System.err(2033):     at java.io.File.createNewFile(File.java:941)
02-15 16:10:50.559: W/System.err(2033):     ... 23 more

我现在正在胡乱猜测,但看起来如下:

File root = Environment.getExternalStorageDirectory();
你忘了打电话了

root.mkdirs();

后来。我的意思是,在你的代码中,你确定你的file+目录存在吗?

我找到了它,以防有人需要它 最终共享映像()


我猜我的代码要小一些,因为它使用Context+MODE\u WORLD\u中的
openFileOutput()
方法读取临时文件,它不需要任何权限:

    ImageView iv = (ImageView )adapter.getmCurrentView();
    String fileName = "image.jpg";
    iv.setDrawingCacheEnabled(true);
    Bitmap bitmap = iv.getDrawingCache();
    try
    {
        FileOutputStream ostream = getActivity().openFileOutput( fileName, Context.MODE_WORLD_READABLE);
        bitmap.compress(Bitmap.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_SUBJECT, "Great photo from Poland!");
    share.putExtra(Intent.EXTRA_TEXT, "Hi,  I'm sharing with you this great picture!");
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile( new File( getActivity().getFileStreamPath( fileName).getAbsolutePath())));
    startActivity(Intent.createChooser(share,"Share via"));

您能告诉我应用程序在Toast中向您显示的确切消息吗?我收到了“出现问题,请重试!”要对它们进行编号以确定显示的是哪一个吗?这一个显示的是catch(例外e){e.printStackTrace();Toast.makeText(getActivity(),“出现问题,请重试!”,Toast.LENGTH_SHORT).show();}您可以使用以“.png”结尾的文件和Bitmap.CompressFormat.JPEG。我认为他们应该匹配。好主意。不幸的是,在API17中不推荐使用Context.MODE_WORLD_READABLE。因此,更新的Andrid版本可能很快就会失败。这不再受支持,并生成异常。是否有其他方法从ImageView创建共享图像?
    ImageView iv = (ImageView )adapter.getmCurrentView();
    String fileName = "image.jpg";
    iv.setDrawingCacheEnabled(true);
    Bitmap bitmap = iv.getDrawingCache();
    try
    {
        FileOutputStream ostream = getActivity().openFileOutput( fileName, Context.MODE_WORLD_READABLE);
        bitmap.compress(Bitmap.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_SUBJECT, "Great photo from Poland!");
    share.putExtra(Intent.EXTRA_TEXT, "Hi,  I'm sharing with you this great picture!");
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile( new File( getActivity().getFileStreamPath( fileName).getAbsolutePath())));
    startActivity(Intent.createChooser(share,"Share via"));