Java tutorial
/******************************************************************************* * Copyright 2013-2015 Karishma Sureka , Sai Gopal , Vijay Teja * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package com.trigger_context.conf; import java.net.URISyntaxException; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.trigger_context.R; public class Set_File_Select extends Activity { String path = ""; private static final int FILE_SELECT_CODE = 0; public static String getPath(Context context, Uri uri) throws URISyntaxException { if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow("_data"); if (cursor.moveToFirst()) { return cursor.getString(column_index); } } catch (Exception e) { // Eat it } } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } private int mid = 1; public void noti(String title, String txt) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(txt); NotificationManager mNotificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(mid++, mBuilder.build()); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case FILE_SELECT_CODE: if (resultCode == RESULT_OK) { // Get the Uri of the selected file Uri uri = data.getData(); // Get the path try { path = getPath(this, uri); EditText et = (EditText) findViewById(R.id.editText1); et.setText(path); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Get the file instance // File file = new File(path); // Initiate the upload } break; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.file_select); final Button done = (Button) findViewById(R.id.button1); done.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); noti("File path :", path); bundle.putBoolean("FileTransferAction", true); bundle.putString("filePath", path); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); Toast.makeText(getApplicationContext(), "Conditions saved!", Toast.LENGTH_SHORT).show(); finish(); } }); final Button browse = (Button) findViewById(R.id.button2); browse.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showFileChooser(); } }); } @Override protected void onResume() { super.onResume(); } @Override protected void onStop() { super.onStop(); } private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); try { startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE); } catch (android.content.ActivityNotFoundException ex) { // Potentially direct the user to the Market with a Dialog Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show(); } } }