radityakurnianto

Code

Aug 14th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. //this is how I assigned an image to image view
  2. @Override
  3.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
  4.     {
  5.         super.onActivityResult(requestCode, resultCode, data);
  6.         if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
  7.         {
  8.             selected_image = data.getData();
  9.             String[] filePath= {MediaStore.Images.Media.DATA};
  10.             cursor = getContentResolver().query(selected_image, filePath, null, null, null);
  11.             cursor.moveToFirst();
  12.            
  13.             int columnIndex = cursor.getColumnIndex(filePath[0]);
  14.             String picturePath = cursor.getString(columnIndex);
  15.             cursor.close();
  16.            
  17.             /*get width and height to rescaled*/
  18.             original_image = BitmapFactory.decodeFile(picturePath);
  19.             int width = original_image.getWidth();
  20.             int height = original_image.getHeight();
  21.             int newWidth = width / 2;
  22.             int newHeight = height / 2;
  23.             Log.i("Before Scale", "Width = " + width + ", Height = " + height);
  24.             Log.i("After Scale", "Width = " + newWidth + ", Height = " + newHeight);
  25.            
  26.             /*scaling image*/
  27.             BitmapFactory.Options image_size = new BitmapFactory.Options();
  28.             original_image.recycle();
  29.             imageUpload = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(picturePath, image_size), newWidth, newHeight, false);
  30.             baos = new ByteArrayOutputStream();
  31.            
  32.             if(original_image.isRecycled() == true)
  33.             {
  34.                 Log.i("I", "True");
  35.             }
  36.            
  37.             if(image.getDrawable() == null)
  38.             {
  39.                 image.setImageBitmap(imageUpload);
  40.                 imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  41.                 image_data_1 = baos.toByteArray();
  42.                 encoded_image_1 = Base64.encodeBytes(image_data_1);
  43.             }
  44.             else if(image2.getDrawable() == null)
  45.             {
  46.                 image2.setImageBitmap(imageUpload);
  47.                 imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  48.                 image_data_2 = baos.toByteArray();
  49.                 encoded_image_2 = Base64.encodeBytes(image_data_2);
  50.             }
  51.             else if(image3.getDrawable() == null)
  52.             {
  53.                 image3.setImageBitmap(imageUpload);
  54.                 imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  55.                 image_data_3 = baos.toByteArray();
  56.                 encoded_image_3 = Base64.encodeBytes(image_data_3);
  57.             }
  58.             else if(image4.getDrawable() == null)
  59.             {
  60.                 image4.setImageBitmap(imageUpload);
  61.                 imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  62.                 image_data_4 = baos.toByteArray();
  63.                 encoded_image_4 = Base64.encodeBytes(image_data_4);
  64.             }
  65.             else if(image5.getDrawable() == null)
  66.             {
  67.                 image5.setImageBitmap(imageUpload);
  68.                 imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  69.                 image_data_5 = baos.toByteArray();
  70.                 encoded_image_5 = Base64.encodeBytes(image_data_5);
  71.             }
  72.             else if(image6.getDrawable() == null)
  73.             {
  74.                 image6.setImageBitmap(imageUpload);
  75.                 imageUpload.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  76.                 image_data_6 = baos.toByteArray();
  77.                 encoded_image_6 = Base64.encodeBytes(image_data_6);
  78.             }
  79.         }
  80.  
  81. //remove image
  82. remove.setOnClickListener(new View.OnClickListener() {
  83.             @Override
  84.             public void onClick(View v) {
  85.                 // TODO Auto-generated method stub
  86.                 if(image6.getDrawable() != null)
  87.                 {
  88.                     image6.setImageBitmap(null);
  89.                     image_data_6 = null;
  90.                 }
  91.                 else if(image5.getDrawable() != null)
  92.                 {
  93.                     image5.setImageBitmap(null);
  94.                     image_data_5 = null;
  95.                 }
  96.                 else if(image4.getDrawable() != null)
  97.                 {
  98.                     image4.setImageBitmap(null);
  99.                     image_data_4 = null;
  100.                 }
  101.                 else if(image3.getDrawable() != null)
  102.                 {
  103.                     image3.setImageBitmap(null);
  104.                     image_data_3 = null;
  105.                 }
  106.                 else if(image2.getDrawable() != null)
  107.                 {
  108.                     image2.setImageBitmap(null);
  109.                     image_data_2 = null;
  110.                 }
  111.                 else if(image.getDrawable() != null)
  112.                 {
  113.                     image.setImageBitmap(null);
  114.                     image_data_1 = null;
  115.                 }
  116.             }
  117.         });
Advertisement
Add Comment
Please, Sign In to add comment