2011-04-13 51 views
1

有人可以帮助我使我的代码存储在缓存而不是我的SD卡。我正在截取我的应用程序的截图,以用于在社交媒体上分享。存储位图在Android缓存中的屏幕截图


// image naming and path to include sd card 
       String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND; 

       // create bitmap screen capture 
       Bitmap bitmap; 
       View v1 = mCurrentUrlMask.getRootView(); 
       v1.setDrawingCacheEnabled(true); 
       bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
       v1.setDrawingCacheEnabled(false); 

       OutputStream fout = null; 
       imageFile = new File(mPath); 

       try { 
        fout = new FileOutputStream(imageFile); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout); 
        fout.flush(); 
        fout.close(); 

       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

这里是我的社会化媒体的代码共享截图:


Intent socialIntent = new Intent(Intent.ACTION_SEND); 
        socialIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
        Uri uri = Uri.fromFile(new File(mPath)); 

        socialIntent.putExtra(Intent.EXTRA_STREAM, uri); 
        socialIntent.setType("image/jpeg"); 

        startActivity(Intent.createChooser(socialIntent, "How do you wanna share?")); 
+1

使用Context.getCacheDir()而不是Environment.getExternalStorageDirectory() – Squonk 2011-04-13 15:13:39

+0

我不相信,我可以使用缓存,我的方案。只有我的应用程序才能访问此存储,并且我需要社交媒体才能访问此存储,并且它不会。 – taraloca 2011-04-13 17:44:34

+1

“我需要社交媒体才能访问这个存储,它不会也不会” - 这是正确的,但您的问题是关于不使用SD卡。如果您希望其他应用程序/活动等访问该文件,则必须将其保存在外部存储器(例如SD卡)中。外部存储不适用文件系统权限,但内部内存不会。 – Squonk 2011-04-13 17:49:34

回答

1

虽然它不是存储在缓存中的位图一个好主意,因为会出现内存泄漏问题。但是,您可以执行以下操作将位图存储在缓存中。

static HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>(); 

存储位图入高速缓存

cache.put(文件名,BMP);

后,要检索缓存使用位图下面的代码,

if(cache.containsKey(filename)){ 
     imageView.setImageBitmap(cache.get(filename)); 
    }