Android 不同图像的不同getPath

Android 不同图像的不同getPath,android,uiimageview,Android,Uiimageview,我需要为不同的图像设置不同的getPath。下面是描述一个图像的getPath的示例。我无法理解如何使用它设置2个图像 public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); if (cursor !

我需要为不同的图像设置不同的getPath。下面是描述一个图像的getPath的示例。我无法理解如何使用它设置2个图像

public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
位图:-

public void decodeFile(String filePath) {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 70;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bitmap = BitmapFactory.decodeFile(filePath, o2);
        bitmap2 = BitmapFactory.decodeFile(filePath, o2);
        imgView.setImageBitmap(bitmap);

        imgView2.setImageBitmap(bitmap2);

不,实际上您不必对多个图像使用不同的getPath,您可以对n个图像使用单个方法

看看下面这个例子

//按钮点击事件我正在打开手机的图库

but1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                openGallery(SELECT_FILE1);
            }
        });

        but2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                openGallery(SELECT_FILE2);
            }
        });
这里有一个开放的画廊功能

public void openGallery(int req_code) {
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(i, req_code);
    }
在onActivity结果中,我设置了不同的路径,从中获取图像

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();

            if (requestCode == SELECT_FILE1) {
                selectedPath1 = getPath(selectedImageUri);
                editText1.setText(selectedPath1);
            }
            if (requestCode == SELECT_FILE2) {
                selectedPath2 = getPath(selectedImageUri);
                editText2.setText(selectedPath2);
            }

        }
    }
最后在这里

public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

您的问题不清楚。我们需要位图来在单击按钮时在imageview上显示图像。我正试图通过上面编写的代码找到它的路径。对于1个图像显示,它可以正常工作,但现在我想显示2个图像,我想需要多构建一个路径。@用户3467204,检查我下面的回答,希望它能帮助您。@InnocentKiller但我们需要为不同的图像创建不同的位图imageviews@user3467204,是的,我们需要不同的位图。