Example usage for android.net Uri getPath

List of usage examples for android.net Uri getPath

Introduction

In this page you can find the example usage for android.net Uri getPath.

Prototype

@Nullable
public abstract String getPath();

Source Link

Document

Gets the decoded path.

Usage

From source file:it.feio.android.omninotes.utils.StorageHelper.java

/**
 * Create a path where we will place our private file on external
 *
 * @param mContext/*from w w  w .  j  a  v a2s.  c  o  m*/
 * @param uri
 * @return
 */
public static File createExternalStoragePrivateFile(Context mContext, Uri uri, String extension) {

    // Checks for external storage availability
    if (!checkStorage()) {
        Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), Toast.LENGTH_SHORT).show();
        return null;
    }
    File file = createNewAttachmentFile(mContext, extension);

    InputStream is;
    OutputStream os;
    try {
        is = mContext.getContentResolver().openInputStream(uri);
        os = new FileOutputStream(file);
        copyFile(is, os);
    } catch (IOException e) {
        try {
            is = new FileInputStream(FileHelper.getPath(mContext, uri));
            os = new FileOutputStream(file);
            copyFile(is, os);
            // It's a path!!
        } catch (NullPointerException e1) {
            try {
                is = new FileInputStream(uri.getPath());
                os = new FileOutputStream(file);
                copyFile(is, os);
            } catch (FileNotFoundException e2) {
                Log.e(Constants.TAG, "Error writing " + file, e2);
                file = null;
            }
        } catch (FileNotFoundException e2) {
            Log.e(Constants.TAG, "Error writing " + file, e2);
            file = null;
        }
    }
    return file;
}

From source file:com.github.snowdream.android.apps.imageviewer.ImageViewerActivity.java

public void initData() {
    imageLoader = ImageLoader.getInstance();
    imageUrls = new ArrayList<String>();

    Intent intent = getIntent();/*from w  w  w .j a  va2 s .  c  o  m*/
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            imageUrls = bundle.getStringArrayList(Extra.IMAGES);
            imagePosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
            imageMode = bundle.getInt(Extra.IMAGE_MODE, 0);
            Log.i("The snowdream bundle path of the image is: " + imageUri);
        }

        Uri uri = (Uri) intent.getData();
        if (uri != null) {
            imageUri = uri.getPath();
            fileName = uri.getLastPathSegment();
            getSupportActionBar().setSubtitle(fileName);
            Log.i("The path of the image is: " + imageUri);

            File file = new File(imageUri);

            imageUri = "file://" + imageUri;
            File dir = file.getParentFile();
            if (dir != null) {
                FileFilter fileFilter = new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        if (f != null) {
                            String extension = MimeTypeMap
                                    .getFileExtensionFromUrl(Uri.encode(f.getAbsolutePath()));
                            if (!TextUtils.isEmpty(extension)) {
                                String mimeType = MimeTypeMap.getSingleton()
                                        .getMimeTypeFromExtension(extension);
                                if (!TextUtils.isEmpty(mimeType) && mimeType.contains("image")) {
                                    return true;
                                }
                            }
                        }
                        return false;
                    }
                };

                File[] files = dir.listFiles(fileFilter);

                if (files != null && files.length > 0) {
                    int size = files.length;

                    for (int i = 0; i < size; i++) {
                        imageUrls.add("file://" + files[i].getAbsolutePath());
                    }
                    imagePosition = imageUrls.indexOf(imageUri);
                    imageMode = 1;
                    Log.i("Image Position:" + imagePosition);
                }

            } else {
                imageUrls.add("file://" + imageUri);
                imagePosition = 0;
                imageMode = 0;
            }
        }
    }

    else

    {
        Log.w("The intent is null!");
    }

}

From source file:com.dycody.android.idealnote.SketchFragment.java

public void save() {
    Bitmap bitmap = mSketchView.getBitmap();
    if (bitmap != null) {

        try {/*  w w w .j  av  a 2  s . c o m*/
            Uri uri = getArguments().getParcelable(MediaStore.EXTRA_OUTPUT);
            File bitmapFile = new File(uri.getPath());
            FileOutputStream out = new FileOutputStream(bitmapFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            if (bitmapFile.exists()) {
                getMainActivity().sketchUri = uri;
            } else {
                getMainActivity().showMessage(R.string.error, ONStyle.ALERT);
            }

        } catch (Exception e) {
            Log.e(Constants.TAG, "Error writing sketch image data", e);
        }
    }
}

From source file:com.deliciousdroid.activity.FragmentBaseActivity.java

@Override
@TargetApi(14)//from w ww . j a v a 2  s.c  om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mContext = this;
    mAccountManager = AccountManager.get(this);
    settings = PreferenceManager.getDefaultSharedPreferences(this);

    loadSettings();
    init();

    if (android.os.Build.VERSION.SDK_INT >= 14) {
        getActionBar().setHomeButtonEnabled(true);
    }

    Intent intent = getIntent();

    if (Intent.ACTION_SEARCH.equals(intent.getAction()) && !intent.hasExtra("MainSearchResults")) {
        if (intent.hasExtra(SearchManager.QUERY)) {
            Intent i = new Intent(this, MainSearchResults.class);
            i.putExtras(intent.getExtras());
            startActivity(i);
            finish();
        } else {
            onSearchRequested();
        }
    } else if (Constants.ACTION_SEARCH_SUGGESTION.equals(intent.getAction())) {
        Uri data = intent.getData();
        String path = null;
        String tagname = null;

        if (data != null) {
            path = data.getPath();
            tagname = data.getQueryParameter("tagname");
        }

        if (data.getScheme() == null || !data.getScheme().equals("content")) {
            Intent i = new Intent(Intent.ACTION_VIEW, data);
            startActivity(i);
            finish();
        } else if (path.contains("bookmarks") && TextUtils.isDigitsOnly(data.getLastPathSegment())
                && intent.hasExtra(SearchManager.USER_QUERY)) {
            Intent viewBookmark = new Intent(this, ViewBookmark.class);
            viewBookmark.setAction(Intent.ACTION_VIEW);
            viewBookmark.setData(data);
            viewBookmark.removeExtra(SearchManager.USER_QUERY);
            Log.d("View Bookmark Uri", data.toString());
            startActivity(viewBookmark);
            finish();
        } else if (tagname != null) {
            Intent viewTags = new Intent(this, BrowseBookmarks.class);
            viewTags.setData(data);

            Log.d("View Tags Uri", data.toString());
            startActivity(viewTags);
            finish();
        }
    }
}

From source file:ca.rmen.android.networkmonitor.app.prefs.PreferenceFragmentActivity.java

@Override
protected void onCreate(Bundle bundle) {
    Log.v(TAG, "onCreate, bundle = " + bundle);
    super.onCreate(bundle);
    String action = getIntent().getAction();
    if (ACTION_IMPORT.equals(action)) {
        // Get the file the user selected, and show a dialog asking for confirmation to import the file.
        Uri importFile = getIntent().getExtras().getParcelable(EXTRA_IMPORT_URI);
        DialogFragmentFactory.showConfirmDialog(this, getString(R.string.import_confirm_title),
                getString(R.string.import_confirm_message, importFile.getPath()), ID_ACTION_IMPORT,
                getIntent().getExtras());
    } else if (ACTION_CHECK_LOCATION_SETTINGS.equals(action)) {
        checkLocationSettings();//from w w w  .j a  v  a  2  s.c  om
    } else if (ACTION_SHOW_INFO_DIALOG.equals(action)) {
        DialogFragmentFactory.showInfoDialog(this, getIntent().getExtras().getString(EXTRA_DIALOG_TITLE),
                getIntent().getExtras().getString(EXTRA_DIALOG_MESSAGE));
    } else if (ACTION_SHOW_WARNING_DIALOG.equals(action)) {
        DialogFragmentFactory.showWarningDialog(this, getIntent().getExtras().getString(EXTRA_DIALOG_TITLE),
                getIntent().getExtras().getString(EXTRA_DIALOG_MESSAGE));
    } else {
        Log.w(TAG, "Activity created without a known action.  Action=" + action);
        finish();
    }
}

From source file:com.google.android.apps.mytracks.io.file.ImportActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();//from   ww  w .  ja  v a2  s. co  m
    importAll = intent.getBooleanExtra(EXTRA_IMPORT_ALL, false);
    trackFileFormat = intent.getParcelableExtra(EXTRA_TRACK_FILE_FORMAT);
    if (trackFileFormat == null) {
        trackFileFormat = TrackFileFormat.GPX;
    }

    if (!FileUtils.isExternalStorageAvailable()) {
        Toast.makeText(this, R.string.external_storage_not_available, Toast.LENGTH_LONG).show();
        finish();
        return;
    }
    if (importAll) {
        path = FileUtils.buildExternalDirectoryPath(trackFileFormat == TrackFileFormat.KML ? "kml" : "gpx");
        if (!FileUtils.isDirectory(new File(path))) {
            Toast.makeText(this, getString(R.string.import_no_directory, path), Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    } else {
        String action = intent.getAction();
        if (!(Intent.ACTION_ATTACH_DATA.equals(action) || Intent.ACTION_VIEW.equals(action))) {
            Log.d(TAG, "Invalid action: " + intent);
            finish();
            return;
        }

        Uri data = intent.getData();
        if (!UriUtils.isFileUri(data)) {
            Log.d(TAG, "Invalid data: " + intent);
            finish();
            return;
        }
        path = data.getPath();
    }

    Object retained = getLastNonConfigurationInstance();
    if (retained instanceof ImportAsyncTask) {
        importAsyncTask = (ImportAsyncTask) retained;
        importAsyncTask.setActivity(this);
    } else {
        importAsyncTask = new ImportAsyncTask(this, importAll, trackFileFormat, path);
        importAsyncTask.execute();
    }
}

From source file:br.com.devfest.norte.wear.HomeListenerService.java

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    LOGD(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());

    for (DataEvent event : dataEvents) {
        Uri uri = event.getDataItem().getUri();
        LOGD(TAG, "onDataChanged(): Received a data item change with uri: " + uri.toString());
        if (event.getType() == DataEvent.TYPE_DELETED) {
            if (uri.getPath().startsWith(PATH_FEEDBACK)) {
                dismissLocalNotification(uri.getLastPathSegment());
            }/* w  w  w  .  ja v a2  s  . c  o  m*/
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            if (uri.getPath().startsWith(PATH_FEEDBACK)) {
                setupNotification(event.getDataItem());
            }
        }
    }
}

From source file:com.ncode.android.apps.schedo.iowear.HomeListenerService.java

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    Utils.LOGD(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());

    for (DataEvent event : dataEvents) {
        Uri uri = event.getDataItem().getUri();
        Utils.LOGD(TAG, "onDataChanged(): Received a data item change with uri: " + uri.toString());
        if (event.getType() == DataEvent.TYPE_DELETED) {
            if (uri.getPath().startsWith(PATH_FEEDBACK)) {
                dismissLocalNotification(uri.getLastPathSegment());
            }/*from w  w w. j  av a 2  s  .c  o m*/
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            if (uri.getPath().startsWith(PATH_FEEDBACK)) {
                setupNotification(event.getDataItem());
            }
        }
    }
}

From source file:com.kenny.Image.ImageGalleryPagerActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_gallery_pager);

    Bundle bundle = getIntent().getExtras();
    assert bundle != null;
    //      imageUrls = bundle.getStringArray(Extra.IMAGES);
    //      int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    // IntentGridView
    Intent intent = getIntent();/*w  ww. j  a  v a2 s .  co  m*/
    Uri uri = (Uri) intent.getData();
    String path = null;
    if (uri != null) {
        path = uri.getPath();
    }
    if (path == null) {
        Log.v("tag", path);
        path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
    }
    imageUrls = setFilePath(path);

    options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_error).resetViewBeforeLoading(true).cacheOnDisc(true)
            .imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);
    pager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            gallery.setSelection(arg0);
            ImageGalleryPagerActivity.this.setTitle(imageUrls.get(arg0));
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

    gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageGalleryAdapter());
    gallery.setSelection(pagerPosition);
    gallery.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startImagePagerActivity(position);
        }
    });

    //      Intent intent = new Intent();
    //      intent.setClassName("com.kenny.KFileManager", "com.kenny.file.Activity.SettingPage");
    //      startActivity(intent);
}

From source file:org.mobisocial.corral.ContentCorral.java

private static void scrapePage(File storageDir, Uri baseUri, String relativePath) throws IOException {
    File sourceFile = new File(storageDir, relativePath);
    String page = IOUtils.toString(new FileInputStream(sourceFile));
    Matcher matcher = sScriptRegex.matcher(page);
    int offset = 0;
    while (matcher.find(offset)) {
        try {//from   ww w  .  j a  v  a 2s.  c o m
            String tag = matcher.group();
            Matcher srcMatcher = sSrcRegex.matcher(tag);
            if (!srcMatcher.find())
                continue;
            String srcPath = srcMatcher.group(1);
            srcPath = srcPath.substring(1, srcPath.length() - 1);
            srcPath = StringEscapeUtils.unescapeHtml4(srcPath);
            //srcPath = absoluteToRelative(baseUri, srcPath);
            if (!srcPath.contains("://")) {
                Uri absolutePath = getAbsoluteUri(baseUri, relativePath, srcPath);
                downloadFile(storageDir, absolutePath, absolutePath.getPath());
            }
        } finally {
            offset = matcher.end();
        }
    }
}