Using Crop intent Getting java.lang.SecurityException: Unable to find app for caller android.app.ApplicationThreadProxy@4266ae80

后端 未结 3 777
南方客
南方客 2020-12-30 06:09

I am using crop intent to crop the image,most of time it run fine,but sometime getting java.lang.SecurityException: Unable to find app for caller android.app.Application

3条回答
  •  生来不讨喜
    2020-12-30 06:31

    I agree with the Roberto Lombardini's answer. Maybe you can try this solution for simple reference.

    1. Save your bitmap into local first with unique name or you own resource id, you can use UUID

      public void savePhotoCache(final String resourceId, final Bitmap bitmap) {
          if(bitmap==null)
              return;
      
          File imageDir = new File(context.getCacheDir(), "images/yourpackagename");
          if (!imageDir.isDirectory())
              imageDir.mkdirs();
      
          File cachedImage = new File(imageDir, resourceId);
          FileOutputStream output = null;
          try {
              output = new FileOutputStream(cachedImage);
              bitmap.compress(PNG, 100, output);
          } catch (IOException e) {
              Log.d("CACHED_IMAGE", "Exception writing cache image", e);
          } finally {
              if (output != null)
                  try {
                      output.close();
                  } catch (IOException e) {
                      // Ignored
                  }
          }
      }
      
    2. When passing to another activity you only pass resource id and get bitmap data in another activity using this function.

      private Bitmap getPhotoCache(final String resourceId) {
          File imageDir = new File(context.getCacheDir(), "images/yourpackagename");
          if (!imageDir.isDirectory())
              imageDir.mkdirs();
      
          File imageFile = new File(imageDir, resourceId);
      
          if (!imageFile.exists() || imageFile.length() == 0)
              return null;
      
          Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
          if (bitmap != null)
              return bitmap;
          else {
              imageFile.delete();
              return null;
          }
      }
      

    I hope this can help you. :)

提交回复
热议问题