解密图像和galleryView

时间:2013-01-09 09:49:37

标签: android encryption android-image

在我的应用中,我将图像相机存储到私人文件夹并加密。现在我想将解密后的图像显示到图库中,不应该存储在任何地方。

图库的代码是

    public class DisplayImage extends Activity{

    private Uri[] mUrls;
    String[] mFiles=null;
    private final String KEY = "abc";
    ImageView imv;
     List<String> tFileList = new ArrayList<String>();
        byte [] decrpt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);

        imv = (ImageView)findViewById(R.id.ImageView01);
        Gallery g = (Gallery) findViewById(R.id.examplegallery);
        g.setAdapter(new ImageAdapter(this, ReadSDCard()));

        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent,
              View v, int position, long id) {

                System.out.println("Positi"+position);
                List<String> FileList = tFileList;

                 Bitmap bm = BitmapFactory.decodeFile(
                          FileList.get(position).toString());
                  imv.setImageBitmap(bm);



            }
        });
        }

        private List<String> ReadSDCard()
        {


         //It have to be matched with the directory in SDCard
         File f = new File("sdcard/data/crak");

         File[] files=f.listFiles();

         for(int i=0; i<files.length; i++)
         {
          File file = files[i];
          /*It's assumed that all file in the path are in supported type*/
          tFileList.add(file.getPath());
         }
         return tFileList;
        }

        public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;
        private List<String> FileList;

        public ImageAdapter(Context c, List<String> fList) {
            mContext = c;
            FileList = fList;
            TypedArray a = obtainStyledAttributes(R.styleable.GalleryTheme);
            mGalleryItemBackground = a.getResourceId(
            R.styleable.GalleryTheme_android_galleryItemBackground,0);
            a.recycle();
        }

        public int getCount() {
            return FileList.size();
        }

        public Object getItem(int position) {
            return position;
        } 

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView,
          ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            Bitmap bm = BitmapFactory.decodeFile(
              FileList.get(position).toString());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] data = baos.toByteArray();


        try {
            CryptClass simpleCrypto = new CryptClass();

            decrpt = simpleCrypto.decrypt(KEY, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Bitmap bitmapimage = BitmapFactory.decodeByteArray(decrpt, 0, decrpt.length); 
            i.setImageBitmap(bitmapimage);

            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);

            return i;
        }
        }
}

还有用于解密的java文件,

    private  byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
}

但它显示null pointer exeption。我怎样才能解决它。

logcat错误消息

   01-09 15:32:25.882: E/AndroidRuntime(17863): FATAL EXCEPTION: main
  01-09 15:32:25.882: E/AndroidRuntime(17863): java.lang.NullPointerException
  01-09 15:32:25.882: E/AndroidRuntime(17863):  at c     com.example.security.DisplayImage$ImageAdapter.getView(DisplayImage.java:110)
 01-09 15:32:25.882: E/AndroidRuntime(17863):   at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
01-09 15:32:25.882: E/AndroidRuntime(17863):    at android.view.View.measure(View.java:8313)
 01-09 15:32:25.882: E/AndroidRuntime(17863):   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)

0 个答案:

没有答案