Guest User

Untitled

a guest
Sep 3rd, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package com.feedabyte.fiton;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.net.Uri;
  7. import android.provider.MediaStore;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.CompoundButton;
  12. import android.widget.Switch;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. public class MainActivity extends Activity {
  17.     private Switch sw;
  18.     private boolean RATIO;
  19.     private String file_path;
  20.     private static final int ACTIVITY_CHOOSE_FILE = 3;
  21.     private TextView tv;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         sw = (Switch) findViewById(R.id.switch1);
  28.         Button clickable = (Button) findViewById(R.id.button);
  29.         tv = (TextView) findViewById(R.id.textView);
  30.         clickable.setOnClickListener(new View.OnClickListener() {
  31.             @Override
  32.             public void onClick(View view) {
  33.                 Intent chooseFile;
  34.                 Intent intent;
  35.                 chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
  36.                 chooseFile.setType("image/*");
  37.                 intent = Intent.createChooser(chooseFile, "Choose Your Picture");
  38.                 startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
  39.                 if (getParent() == null) {
  40.                     setResult(Activity.RESULT_OK, intent);
  41.                 } else {
  42.                     getParent().setResult(Activity.RESULT_OK, intent);
  43.                 }
  44.             }
  45.         });
  46.  
  47.         //Switch Click get Function
  48.         sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  49.             //@Override
  50.             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  51.                 if (RATIO = b) {
  52.                     Toast.makeText(getApplicationContext(), R.string.switchOn, Toast.LENGTH_LONG).show();
  53.                     tv.setText(R.string.switchOn);
  54.                 } else {
  55.                     tv.setText(R.string.switchDefault);
  56.                 }
  57.             }
  58.         });
  59.     }
  60.     @Override
  61.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  62.         super.onActivityResult(requestCode, resultCode, data);
  63.         Toast.makeText(getApplicationContext(), getRealPathFromURI(data.getData())+" and "+resultCode+" and "+requestCode,Toast.LENGTH_LONG).show();
  64.         if (requestCode == ACTIVITY_CHOOSE_FILE) {
  65.             if (resultCode != Activity.RESULT_OK) return;
  66.             Uri uri = data.getData();
  67.             file_path = getRealPathFromURI(uri);
  68.         }
  69.     }
  70.     //Get complete file Path
  71.     public String getRealPathFromURI(Uri contentUri) {
  72.         String [] proj      = {MediaStore.Images.Media.DATA};
  73.         Cursor cursor       = getContentResolver().query( contentUri, proj, null, null,null);
  74.         if (cursor == null) return null;
  75.         int column_index    = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  76.         cursor.moveToFirst();
  77.         String temp_return_string = cursor.getString(column_index);
  78.         cursor.close();
  79.         return temp_return_string;
  80.     }
  81. }
Add Comment
Please, Sign In to add comment