截图后使用图库编辑打开图片报错问题

Android系统截图后,点击截图后出现的编辑按钮,会调用图库打开,这个时候会出现闪退报错的现象。具体报错如下:

AndroidRuntime: FATAL EXCEPTION: main
AndroidRuntime: Process: com.android.gallery3d, PID: 2108
AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int)' on a null object reference
AndroidRuntime: at com.android.gallery3d.filtershow.FilterShowActivity.loadXML(FilterShowActivity.java:352)
AndroidRuntime: at com.android.gallery3d.filtershow.FilterShowActivity.updateUIAfterServiceStarted(FilterShowActivity.java:263)
AndroidRuntime: at com.android.gallery3d.filtershow.pipeline.ProcessingService.onStart(ProcessingService.java:232)
AndroidRuntime: at com.android.gallery3d.filtershow.FilterShowActivity$1.onServiceConnected(FilterShowActivity.java:217)
AndroidRuntime: at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2188)
AndroidRuntime: at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2221)
AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:974)
AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201)
AndroidRuntime: at android.os.Looper.loop(Looper.java:288)
AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7921)
AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1149)

此问题是由于图库在系统没有触摸屏的时候不会加载actionBar,导致空指针出现。解决方法就是针对报错进行规避即可。具体修改如下:

--- a/packages/apps/Gallery2/src/com/android/gallery3d/filtershow/FilterShowActivity.java
+++ b/packages/apps/Gallery2/src/com/android/gallery3d/filtershow/FilterShowActivity.java
@@ -125,6 +125,7 @@ import java.io.FileOutputStream;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Vector;
+import android.view.Window;

 public class FilterShowActivity extends FragmentActivity implements OnItemClickListener,
         OnShareTargetSelectedListener, DialogInterface.OnShowListener,
@@ -349,18 +350,21 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL
         setContentView(R.layout.filtershow_activity);

         ActionBar actionBar = getActionBar();
 public class FilterShowActivity extends FragmentActivity implements OnItemClickListener,
         OnShareTargetSelectedListener, DialogInterface.OnShowListener,
@@ -349,18 +350,21 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL
         setContentView(R.layout.filtershow_activity);

         ActionBar actionBar = getActionBar();
-        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
-        actionBar.setCustomView(R.layout.filtershow_actionbar);
-        actionBar.setBackgroundDrawable(new ColorDrawable(
-                getResources().getColor(R.color.background_screen)));
+               if(actionBar != null){
+                       actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
+                       actionBar.setCustomView(R.layout.filtershow_actionbar);
+                       actionBar.setBackgroundDrawable(new ColorDrawable(
+                                       getResources().getColor(R.color.background_screen)));
+
+                       mSaveButton = actionBar.getCustomView();
+                       mSaveButton.setOnClickListener(new OnClickListener() {
+                               @Override
+                               public void onClick(View view) {
+                                       saveImage();
+                               }
+                       });
+               }

-        mSaveButton = actionBar.getCustomView();
-        mSaveButton.setOnClickListener(new OnClickListener() {
-            @Override
-            public void onClick(View view) {
-                saveImage();
-            }
-        });

         mImageShow = (ImageShow) findViewById(R.id.imageShow);
         mImageViews.add(mImageShow);
@@ -493,7 +497,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL
     private void processIntent() {
         Intent intent = getIntent();
         if (intent.getBooleanExtra(LAUNCH_FULLSCREEN, false)) {
-            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
+            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
         }

         mAction = intent.getAction();


  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 设备上,如果您将图片保存到“DCIM/Camera”文件夹中,但是在图库中无法查看,可能是因为图库没有扫描该文件夹。要解决此问题,您可以使用 `@ionic-native/file` 和 `@ionic-native/file-path` 插件将图片复制到图库文件夹中,然后通知图库更新媒体库。 首先,您需要安装这两个插件: ``` ionic cordova plugin add cordova-plugin-file npm install @ionic-native/file ionic cordova plugin add cordova-plugin-filepath npm install @ionic-native/file-path ``` 然后在您的组件中导入这两个插件: ``` import { File } from '@ionic-native/file/ngx'; import { FilePath } from '@ionic-native/file-path/ngx'; ``` 在保存图片之后,您可以使用以下代码将其复制到图库文件夹中: ``` import { Platform } from '@ionic/angular'; constructor(private file: File, private filePath: FilePath, private platform: Platform) {} ... const sourceDirectory = this.file.dataDirectory; // 图片来源目录 const sourceFileName = 'my-image.jpg'; // 图片文件名 const destinationDirectory = this.platform.is('ios') ? this.file.documentsDirectory : this.file.externalRootDirectory + 'DCIM/Camera'; // 图库文件夹 const destinationFileName = new Date().getTime() + '.jpg'; // 新的文件名 this.file.copyFile(sourceDirectory, sourceFileName, destinationDirectory, destinationFileName).then( (success) => { // 文件已成功复制到图库文件夹中 // 现在您需要通知图库更新媒体库 this.filePath.resolveNativePath(destinationDirectory + '/' + destinationFileName).then( (filePath) => { this.platform.ready().then(() => { cordova.plugins.MediaScannerPlugin.scanFile(filePath, () => { console.log('媒体库已更新'); }, (error) => { console.error('无法更新媒体库', error); }); }); }, (error) => { console.error('无法解析文件路径', error); } ); }, (error) => { console.error('无法复制文件', error); } ); ``` 这将复制图片图库文件夹中,并使用 `MediaScannerPlugin` 插件通知媒体库更新。现在您应该能够在图库中看到新的图片了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值