简体   繁体   English

如何从图库中获取图像,获取其名称,将其保存在内部存储器中以及获取该图像的路径

[英]How to get an image from the gallery, get its name, save it in the internal memory and get the route of that image

(I apologize if I make mistakes writing in English) I need to do the same as the title says I only know how to get the image from the gallery. (如果我用英语写错了,我深表歉意)我需要做的事情与标题所说的一样,我只知道如何从图库中获取图像。 I think this is correct: 我认为这是正确的:

(If it is wrong or it is inefficient please let me know) (如果错误或效率低下,请告诉我)

    private final int SELECT_PICTURE = 200;

btnAddImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galeryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            galeryIntent.setType("image/*");
            startActivityForResult(galeryIntent.createChooser(galeryIntent, "Sececiona Imagen"), SELECT_PICTURE);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode){
        case (SELECT_PICTURE):
            if(requestCode == RESULT_OK){
                Uri path = data.getData();
                }
            }
            break;
    }
}

But I don't know how to get the name of the image that I caught from the gallery and save it whit that name in the internal memory of my app. 但是我不知道如何获取从图库中捕获的图像的名称,并将其保存在应用程序的内部存储器中。 Then I need the route of the image like "MyAPP/media/ ..." , I don't know the correct name in English for that, and save it in my database (save it in my database is easy) and then use it in my project 然后,我需要像“ MyAPP / media / ...”之类的图像的路径,我不知道英语的正确名称,然后将其保存在数据库中(将其保存在数据库中很容易),然后使用在我的项目中

I know this is a long message but I need your help because in the Spanish forum of StackOverflow anybody answered me. 我知道这是一条很长的信息,但是我需要您的帮助,因为在StackOverflow西班牙论坛中,有人回答了我。

onActivityResult : onActivityResult:

String path = getPath(getApplicationContext(), data.getData());    
File mFile = new File(id);
mFile.getName();

That is the way that a got the name of the file. 这就是获取文件名的方法。

And the getPath : 和getPath:

    public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Log.d("Lucas","TYPE => "+type);

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }
        } else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        } else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];



            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        return getDataColumn(context, uri, null, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

public static String getDataColumn(Context context, Uri uri, String selection,
                                   String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {
            column
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

public static boolean isExternalStorageDocument(Uri uri) {
    return "com.android.externalstorage.documents".equals(uri.getAuthority());
}

public static boolean isDownloadsDocument(Uri uri) {
    return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}

public static boolean isMediaDocument(Uri uri) {
    return "com.android.providers.media.documents".equals(uri.getAuthority());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM