项目需要图片的64位加密
主要是这个代码:
byte[] encode = Base64.encode(bytes, Base64.DEFAULT);
全部:
/** * 图片64位加密 相册 * * @param data */ private void encode(Intent data) { Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())); } catch (FileNotFoundException e) { e.printStackTrace(); } Log.d("TAG", "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); //base64 encode byte[] encode = Base64.encode(bytes, Base64.DEFAULT); encodeString = new String(encode); } /** * 图片64位加密 相机 * * @param path */ private void encode(File path) { Bitmap bitmap = BitmapFactory.decodeFile(path.getPath()); //Log.d("TAG", "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); //base64 encode byte[] encode = Base64.encode(bytes, Base64.DEFAULT); encodeString = new String(encode); }
/** * 调用系统相机拍照 */ private void startCamera() { Intent intent = new Intent(); //启动相机的Action intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); //文件的保存位置 file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } //设置图片拍摄后保存的位置 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); //启动相机,这里使用有返回结果的启动 startActivityForResult(intent, PICTURE_FROM_CAMERA_S); }相册
tvFile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); //设置启动相册的Action intent.setAction(Intent.ACTION_GET_CONTENT); //设置类型 intent.setType("image/*"); //启动相册,这里使用有返回结果的启动 startActivityForResult(intent, PICTURE_FROM_GALLERY_S); mPopWindow.dismiss(); } });
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case PICTURE_FROM_CAMERA_S: encode(file);//加密 postSendFileGame(encodeString); break; case PICTURE_FROM_GALLERY_S: encode(data);//加密//加密 postSendFileGame(encodeString); break; } } }
这样可能导致内存溢出:解决办法
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 4; bitmap = BitmapFactory.decodeFile(path.getPath() , bitmapOptions);
/** * 图片64位加密 相册 * * @param data */ @TargetApi(Build.VERSION_CODES.KITKAT) private void encode(Intent data) { Bitmap bitmap = null; BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 4; try { bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(data.getData()), null , bitmapOptions); } catch (FileNotFoundException e) { e.printStackTrace(); } //Log.d("TAG", "bitmap width: " + bitmap.getWidth() + " height: " + bitmap.getHeight()+" bitmap.size"+bitmap.getAllocationByteCount()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes =baos.toByteArray(); //base64 encode byte[] encode = Base64.encode(bytes, Base64.DEFAULT); encodeString = new String(encode); } /** * 图片64位加密 相机 * * @param path */ private void encode(File path) { Bitmap bitmap = null; BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 4; bitmap = BitmapFactory.decodeFile(path.getPath() , bitmapOptions); //Bitmap bitmap = BitmapFactory.decodeFile(path.getPath()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] bytes = baos.toByteArray(); //base64 encode byte[] encode = Base64.encode(bytes, Base64.DEFAULT); encodeString = new String(encode); }