开发者

How to save and show bitmap in Android?

So I have a little problem.I can't find how to save a bitmap file after decryptation in sdcard or somewhere else where I can view the image (no matter where).The code I am using now is :

public class SimpleCryptoActivity extends Activity {
    private static final int IO_BUFFER_SIZE = 4 * 1024;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                try {
                    AssetManager am = this.getAssets();
                    InputStream is = am.open("2000_1.jpg_encrypted"); // get the encrypted image from assets folder

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    byte[] b = new byte[IO_BUFFER_SIZE];  
                    int read;  
                    while ((read = is.read(b)) != -1) {  //convert inputstream to bytearrayoutputstream
                        baos.write(b, 0, read);
                    }                           
                long start = System.currentTimeMillis()/1000L; // start

                    byte[] keyStart = "MARTIN_123_MARTIN_123".getBytes();  // specific key value 
                    KeyGenerator kgen = KeyGenerator.getInstance("AES");    //aes
                    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
                    sr.setSeed(keyStart);
                    kgen.init(128, sr); 
                    SecretKey skey = kgen.generateKey();
                    byte[] key = skey.getEncoded();    

                    byte[] decryptedData = decrypt(key,b);  //decryption
                long end = System.currentTimeMillis()/1000L;    // end
                Log.i("TEST","Time start "+ String.valueOf(start)); //showing the strat in ms
                Log.i("TEST","Time end "+ String.valueOf(end));     //showing the end in ms
                    Bitmap bitmap = BitmapFactory.decodeByteArray(b , 0, b .length);    //decoding bytearrayoutputstream to bitmap



                    is.close(); // close the inputstream
                    baos.close(); // close the bytearrayoutputstream
                }
                catch(Exception e){
                    e.fillInStackTrace();
                }
            } 

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

            return decrypted;
            }
}

Another question too.I'm curious why I can't see the

Log.i("TEST","Time start "+ String.valueOf(start)); //showing the strat in ms
                Log.i("TEST","Time end "+ String.valueOf(end));     //showing the end in ms

in the LogCat.Am I doing something wrong or...?Thanks for help开发者_如何学运维!


If it is not important if you store it on the SD or any storage at all. You can add ImageView in your layout and do

ImageView mydecrptimg = (ImageView)findViewById(R.id.imageview);

mydecrptimg.setImageBitmap(yourbitmap);

or store it to MediaStore. Read more here about this provider

http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html

Edit: put this class inside your current class, then instantiate for your ImageView

 private class DrawMyBitmap extends ImageView{

        private Bitmap bmp = null;
        public DrawMyBitmap(Context context, Bitmap mybmp) {
            super(context);
            this.bmp=mybmp;
        }

        @Override
        protected void onDraw(Canvas canvas) {

             Paint paint = new Paint();

             paint.setFilterBitmap(true);
             paint.setAntiAlias(true);

             Rect bmprect = new Rect(0 ,0 , bmp.getHeight(), bmp.getWidth() );
             canvas.drawBitmap(bmp, null, bmprect, paint);
            super.onDraw(canvas);
        }


Where do you set the file that you want to write too?

Do you have the WRITE_EXTERNAL_STORAGE permission in your manifest?

Maybe the Logcat has lost focus, go to the DDMS perspective and click on the emulator on that your app is running. Also set the Log output level on Debug on the right end of the Logcat console.

Edit: to write bytes to a file:

    FileOutputStream fos = new FileOutputStream(strFilePath);
    fos.write(bytes);
    fos.close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜