gpt4 book ai didi

android - 分享和保存 ImageView 的图像?

转载 作者:太空狗 更新时间:2023-10-29 16:03:56 25 4
gpt4 key购买 nike

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

这是保存按钮:

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 上没有显示任何内容,我只是得到了一些错误的 toast 。权限正确。

日志

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

最佳答案

我找到它以防万一有人需要它最终shareimage()

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!1", 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();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(img));
startActivity(Intent.createChooser(share,"Share via"));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Something Went Wrong, Please Try Again!2", Toast.LENGTH_SHORT).show();
}
}

关于android - 分享和保存 ImageView 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799934/

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