Advertisement
Guest User

Untitled

a guest
Jul 17th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1.  
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import static android.content.Intent.*;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17.     static final int PICK_FILE = -1;
  18.     String sciezka = "cos na poczatek";
  19.     private static final String TAG1 = "TAG1_INTENT";
  20.     private static final String TAG2 = "TAG2_ON_ACTIVITY_RESULT";
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         pickFile();
  26.         TextView tv = new TextView(this);
  27.         tv.setText(sciezka);
  28.         setContentView(tv);
  29.  
  30.  
  31.     }
  32.  
  33.     private void pickFile(){
  34.         //Toast.makeText(this, sciezka, Toast.LENGTH_SHORT).show();
  35.         Log.d(TAG1, "Tworzenie nowego intenta");
  36.         Intent intent = new Intent();
  37.         Log.d(TAG1, "ustawianie typow");
  38.         intent.setType("*/*");
  39.         Log.d(TAG1, "ustawienie akcji na get_content");
  40.         intent.setAction(ACTION_GET_CONTENT);
  41.         Log.d(TAG1, "Odpalanie intenta");
  42.         try{
  43.             startActivityForResult(Intent.createChooser(intent, "Wybierz plik"), PICK_FILE);
  44.         } catch (android.content.ActivityNotFoundException anfe){
  45.             Toast.makeText(this, "Zainstaluj przeglądarke plików", Toast.LENGTH_SHORT);
  46.             Log.d(TAG1, "Brak przeglądarki plików");
  47.         } catch (Exception e){
  48.             Log.d(TAG1, "No odpalenie intenta nie zadziałało");
  49.         }
  50.         Toast.makeText(this, "nanana batman", Toast.LENGTH_SHORT);
  51.  
  52.     }
  53.  
  54.     @Override
  55.     protected void onActivityResult(int reqCode, int resCode, Intent data){
  56.         Log.d(TAG2, "Pierwszy if - reqCode == PICK_FILE");
  57.         if(reqCode == PICK_FILE){
  58.             Log.d(TAG2, "Drugi if - resCode == RESULT_CANCELED");
  59.             if(resCode == RESULT_CANCELED){
  60.                 Log.d(TAG2, "wywolanie RESULT_CANCELED - na pewno ok?");
  61.             }
  62.  
  63.             if(resCode == RESULT_OK){
  64.                 Log.d(TAG2, "No to jestesmy w resCode == RESULT_OK, pobieranie danych o selectedFile, tworzenie uri selectedFile");
  65.                 Uri selectedFile = null;
  66.                 Log.d(TAG2, "proba pobrania danych do selectedFile");
  67.                 try{
  68.                     selectedFile = data.getData();
  69.                 } catch (Exception e){
  70.                     Log.d(TAG2, "Cos nie zabanglalo i nie pobralo info");
  71.                 }
  72.                 Log.d(TAG2, "dane pobralo poprawnie, ustawianie path na sciezeczka");
  73.                 String path = "sciezeczka";
  74.                 Log.d(TAG2, "proba pobrania sciezeczki do pliczku");
  75.                 try{
  76.                     path = getFilePath(getApplicationContext(), selectedFile);
  77.                 } catch (Exception e){
  78.                     Log.d(TAG2, "nope, nie pobralo sciezki");
  79.                     path = "No i kiszka";
  80.                 }
  81.                 Log.d(TAG2, "wypisanie patha");
  82.                 System.out.println(path);
  83.  
  84.             }
  85.         } else {
  86.             Log.d(TAG2, "No i pierwszy if nie byl true");
  87.         }
  88.         Log.d(TAG2, "wyswietlenie toasta - tylko z czym?");
  89.         Toast.makeText(this, sciezka, Toast.LENGTH_SHORT).show();
  90.     }
  91.  
  92.     @Override
  93.     public boolean onCreateOptionsMenu(Menu menu) {
  94.         // Inflate the menu; this adds items to the action bar if it is present.
  95.         getMenuInflater().inflate(R.menu.menu_main, menu);
  96.         return true;
  97.     }
  98.  
  99.     @Override
  100.     public boolean onOptionsItemSelected(MenuItem item) {
  101.         // Handle action bar item clicks here. The action bar will
  102.         // automatically handle clicks on the Home/Up button, so long
  103.         // as you specify a parent activity in AndroidManifest.xml.
  104.         int id = item.getItemId();
  105.  
  106.         //noinspection SimplifiableIfStatement
  107.         if (id == R.id.action_settings) {
  108.             return true;
  109.         }
  110.  
  111.         return super.onOptionsItemSelected(item);
  112.     }
  113.  
  114.     public String getFilePath(Context context, Uri uri){
  115.  
  116.         return "cos";
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement