Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- import android.widget.Toast;
- import static android.content.Intent.*;
- public class MainActivity extends AppCompatActivity {
- static final int PICK_FILE = -1;
- String sciezka = "cos na poczatek";
- private static final String TAG1 = "TAG1_INTENT";
- private static final String TAG2 = "TAG2_ON_ACTIVITY_RESULT";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- pickFile();
- TextView tv = new TextView(this);
- tv.setText(sciezka);
- setContentView(tv);
- }
- private void pickFile(){
- //Toast.makeText(this, sciezka, Toast.LENGTH_SHORT).show();
- Log.d(TAG1, "Tworzenie nowego intenta");
- Intent intent = new Intent();
- Log.d(TAG1, "ustawianie typow");
- intent.setType("*/*");
- Log.d(TAG1, "ustawienie akcji na get_content");
- intent.setAction(ACTION_GET_CONTENT);
- Log.d(TAG1, "Odpalanie intenta");
- try{
- startActivityForResult(Intent.createChooser(intent, "Wybierz plik"), PICK_FILE);
- } catch (android.content.ActivityNotFoundException anfe){
- Toast.makeText(this, "Zainstaluj przeglądarke plików", Toast.LENGTH_SHORT);
- Log.d(TAG1, "Brak przeglądarki plików");
- } catch (Exception e){
- Log.d(TAG1, "No odpalenie intenta nie zadziałało");
- }
- Toast.makeText(this, "nanana batman", Toast.LENGTH_SHORT);
- }
- @Override
- protected void onActivityResult(int reqCode, int resCode, Intent data){
- Log.d(TAG2, "Pierwszy if - reqCode == PICK_FILE");
- if(reqCode == PICK_FILE){
- Log.d(TAG2, "Drugi if - resCode == RESULT_CANCELED");
- if(resCode == RESULT_CANCELED){
- Log.d(TAG2, "wywolanie RESULT_CANCELED - na pewno ok?");
- }
- if(resCode == RESULT_OK){
- Log.d(TAG2, "No to jestesmy w resCode == RESULT_OK, pobieranie danych o selectedFile, tworzenie uri selectedFile");
- Uri selectedFile = null;
- Log.d(TAG2, "proba pobrania danych do selectedFile");
- try{
- selectedFile = data.getData();
- } catch (Exception e){
- Log.d(TAG2, "Cos nie zabanglalo i nie pobralo info");
- }
- Log.d(TAG2, "dane pobralo poprawnie, ustawianie path na sciezeczka");
- String path = "sciezeczka";
- Log.d(TAG2, "proba pobrania sciezeczki do pliczku");
- try{
- path = getFilePath(getApplicationContext(), selectedFile);
- } catch (Exception e){
- Log.d(TAG2, "nope, nie pobralo sciezki");
- path = "No i kiszka";
- }
- Log.d(TAG2, "wypisanie patha");
- System.out.println(path);
- }
- } else {
- Log.d(TAG2, "No i pierwszy if nie byl true");
- }
- Log.d(TAG2, "wyswietlenie toasta - tylko z czym?");
- Toast.makeText(this, sciezka, Toast.LENGTH_SHORT).show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public String getFilePath(Context context, Uri uri){
- return "cos";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement