如何从Android中的文件路径获取文件名

曼尼坎丹K

我想从sdcard文件路径获取文件名。例如:/storage/sdcard0/DCIM/Camera/1414240995236.jpg I want get 1414240995236.jpg我已经编写了获取相同代码的代码,但是它不起作用。请帮忙。
下面是我的代码:

@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data)
{
    if ( requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

        if ( resultCode == RESULT_OK) {

            /*********** Load Captured Image And Data Start ****************/

            String imageId = convertImageUriToFile( imageUri,CameraActivity);


            //  Create and excecute AsyncTask to load capture image

            new LoadImagesFromSDCard().execute(""+imageId);

            /*********** Load Captured Image And Data End ****************/


        } else if ( resultCode == RESULT_CANCELED) {

            Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show();
        } else {

            Toast.makeText(this, " Picture was not taken ", Toast.LENGTH_SHORT).show();
        }
    }
}


/************ Convert Image Uri path to physical path **************/

public static String convertImageUriToFile ( Uri imageUri, Activity activity )  {

    Cursor cursor = null;
    int imageID = 0;

    try {

        /*********** Which columns values want to get *******/
        String [] proj={
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Thumbnails._ID,
                MediaStore.Images.ImageColumns.ORIENTATION
        };

        cursor = activity.managedQuery(

                imageUri,         //  Get data for specific image URI
                proj,             //  Which columns to return
                null,             //  WHERE clause; which rows to return (all rows)
                null,             //  WHERE clause selection arguments (none)
                null              //  Order-by clause (ascending by name)

                );

        //  Get Query Data

        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
        int columnIndexThumb = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        //int orientation_ColumnIndex = cursor.
        //    getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);

        int size = cursor.getCount();

        /*******  If size is 0, there are no images on the SD Card. *****/

        if (size == 0) {


            imageDetails.setText("No Image");
        }
        else
        {

            int thumbID = 0;
            if (cursor.moveToFirst()) {

                /**************** Captured image details ************/

                /*****  Used to show image on view in LoadImagesFromSDCard class ******/
                imageID     = cursor.getInt(columnIndex);

                thumbID     = cursor.getInt(columnIndexThumb);

                String Path = cursor.getString(file_ColumnIndex);

                //String orientation =  cursor.getString(orientation_ColumnIndex);

                String CapturedImageDetails = " CapturedImageDetails : \n\n"
                        +" ImageID :"+imageID+"\n"
                        +" ThumbID :"+thumbID+"\n"
                        +" Path :"+Path+"\n";
                full_path_name=Path;



       //this is my path  
       //Path :/storage/sdcard0/DCIM/Camera/1414240995236.jpg  i want get 1414240995236.jpg








                // Show Captured Image detail on activity
                imageDetails.setText(Path);

            }
        }   
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    // Return Captured Image ImageID ( By this ImageID Image will load from sdcard )

    return ""+imageID;
}


/**
 * Async task for loading the images from the SD card.
 *
 * @author Android Example
 *
 */

// Class with extends AsyncTask class

public class LoadImagesFromSDCard  extends AsyncTask<String, Void, Void> {

    private ProgressDialog Dialog = new ProgressDialog(CameraPhotoCapture.this);

    Bitmap mBitmap;

    protected void onPreExecute() {
        /****** NOTE: You can call UI Element here. *****/

        // Progress Dialog
        Dialog.setMessage(" Loading image from Sdcard..");
        Dialog.show();
    }


    // Call after onPreExecute method
    protected Void doInBackground(String... urls) {

        Bitmap bitmap = null;
        Bitmap newBitmap = null;
        Uri uri = null;      


        try {

            /**  Uri.withAppendedPath Method Description
             * Parameters
             *    baseUri  Uri to append path segment to
             *    pathSegment  encoded path segment to append
             * Returns
             *    a new Uri based on baseUri with the given segment appended to the path
             */

            uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]);

            /**************  Decode an input stream into a bitmap. *********/
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

            if (bitmap != null) {

                /********* Creates a new bitmap, scaled from an existing bitmap. ***********/

                newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true);

                bitmap.recycle();

                if (newBitmap != null) {

                    mBitmap = newBitmap;
                }
            }
        } catch (IOException e) {
            // Error fetching image, try to recover

            /********* Cancel execution of this task. **********/
            cancel(true);
        }

        return null;
    }


    protected void onPostExecute(Void unused) {

        // NOTE: You can call UI Element here.

        // Close progress dialog
        Dialog.dismiss();

        if(mBitmap != null)
        {
            // Set Image to ImageView 

            showImg.setImageBitmap(mBitmap);
        } 

    }

}
肥仔

我认为您可以使用子字符串方法从路径字符串获取文件名。

String path=":/storage/sdcard0/DCIM/Camera/1414240995236.jpg"; 
// it contains your image path...I'm using a temp string...
String filename=path.substring(path.lastIndexOf("/")+1);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

获取目录路径中的文件名

来自分类Dev

从路径获取文件名

来自分类Dev

如何使用jQuery从输入类型文件中获取选定的文件名和文件路径

来自分类常见问题

如何快速从文件路径获取文件名

来自分类Dev

如何拆分文件路径以获取文件名?

来自分类Dev

如何从存储在HDFS分布式缓存中的文件的路径中获取文件名

来自分类Dev

获取不带文件名的文件的路径

来自分类Dev

如何从ImageView(ANDROID)获取文件名

来自分类Dev

如何从ImageView(ANDROID)获取文件名

来自分类Dev

如何获取在Java程序中打开的完整文件名和文件路径?

来自分类Dev

如何从具有可变文件名的查找中获取文件路径

来自分类Dev

如何从没有扩展名的路径中获取特定文件名python

来自分类Dev

如何使用C ++从整个路径获取文件名

来自分类Dev

如何从Shell变量获取路径的文件名部分

来自分类Dev

如何从 ls 获取全名、路径和文件名?

来自分类Dev

从R中的url路径获取文件名

来自分类Dev

从R中的url路径获取文件名

来自分类Dev

如何在Python中从具有完整路径的变量中获取文件名?

来自分类Dev

如何在eclipse插件中获取选定的文件名及其包路径

来自分类Dev

如何基于父标记中的给定属性获取文件名和路径

来自分类Dev

仅从给定路径获取文件名

来自分类Dev

从路径批量获取文件名

来自分类Dev

剥离目录路径以获取文件名

来自分类Dev

从图像路径(从函数)获取文件名

来自分类Dev

获取路径并附加文件名

来自分类Dev

获取对象的路径和文件名

来自分类Dev

如何在Perl中从文件名获取文件句柄?

来自分类Dev

如何从文件字段中获取文件名?

来自分类Dev

如何使用PowerShell中的文件内容获取文件名

Related 相关文章

热门标签

归档