Sowmya
Sowmya

Reputation: 1

Need to pass a compressed image for encryption in Android

We are building an application which picks an image from the gallery displays it in imageView. When the encrypt button is pressed it is passed to a java class where it gets encrypted and is stored in the memory. I want to compress the image before encrypting. I am using Bitmap.scaledimage but I dont know exactly where to add it

Code for picking the image:

  public void onClick(View v)
    {
        Intent i= new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,SELECTED_PICTURE);
    }
    });

code for displaying the image:

 protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode)
    {
    case SELECTED_PICTURE:
        if(resultCode==RESULT_OK)
        {

            Uri uri= data.getData();
            String[]projection= {MediaStore.Images.Media.DATA};                                                                   
            Cursor cursor= getContentResolver().query(uri, projection, null, null, null);
            cursor.moveToFirst();
            int columnIndex= cursor.getColumnIndex(projection[0]);
            filePath= cursor.getString(columnIndex);
            cursor.close();
            iv.setImageBitmap(BitmapFactory.decodeFile(filePath));
            }
        break;

code for encrypting:

one = (Button) findViewById(R.id.button2);
    one.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try{
                blowfish encryptFile = new blowfish("thisismypassword");
                //destpath=Environment.getExternalStorageDirectory().getAbsolutePath()+nameoffolder;
                destpath= x.getPath()+nameoffolder;
                //destpath="/storage/sdcard/test";
                encryptFile.encrypt(filePath,destpath);
                Toast.makeText(Image.this,
                        "Done encrypting..yey..",
                        Toast.LENGTH_SHORT).show();

                }catch (Exception e) {
                    Toast.makeText(Image.this, e.getMessage(),
                            Toast.LENGTH_SHORT).show();System.out.println("sells");
            }
        }
    });

File x is declared like this:

final File x=new File(android.os.Environment.getExternalStorageDirectory(),File.separator+"Image");
    x.mkdirs();

FilePath is string.

I want to know where I can add the image compression (what part of the code). Image compression we need bitmap but throughout I am working with uri, files and string for storing and displaying.

Upvotes: 0

Views: 380

Answers (1)

kuciyang
kuciyang

Reputation: 21

public Bitmap decodeSampledBitmapFromUri(String uri, int reqWidth, int reqHeight) {

    Bitmap bm = null;

    try{
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(uri)), null, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(uri)), null, options);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }

    return bm;
}

public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

might be like this. may i see your full coding? i need your encrypt and decrypt part.

Upvotes: 1

Related Questions