0

我正在尝试编写一个应用程序,允许用户将图片上传到服务器。现在我有一个关于图像路径的问题。我想在编辑文本中显示图像的完整路径。当我尝试时,我在编辑文本中得到 NULL 而不是完整路径。图片数量不固定。有人可以帮助我吗?谢谢!

我的屏幕截图是 http://i.share.pho.to/7255279b_o.png

当我按下添加按钮电话库打开时,我正在选择图像,我想在编辑文本中显示图像的完整路径。

我的代码

public class Manual extends Activity {

    Button but1, but2, but3, but4, upload;
    EditText editText1, editText2, editText3, editText4;
    private static final int SELECT_FILE1 = 1;
    private static final int SELECT_FILE2 = 2;
    private static final int SELECT_FILE3 = 3;
    private static final int SELECT_FILE4 = 4;
    String selectedPath1;
    String selectedPath2;
    String selectedPath3;
    String selectedPath4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.manual);

        but1 = (Button) findViewById(R.id.button1);
        but2 = (Button) findViewById(R.id.button2);
        but3 = (Button) findViewById(R.id.button3);
        but4 = (Button) findViewById(R.id.button4);
        upload = (Button) findViewById(R.id.button5);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);

        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);
            }
        });

        but3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                openGallery(SELECT_FILE3);
            }
        });

        but4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                openGallery(SELECT_FILE4);
            }
        });

    }

    public void openGallery(int req_code) {
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        final int ACTIVITY_SELECT_IMAGE = 1234;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
    }

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

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

            if (requestCode == SELECT_FILE1) {
                selectedPath1 = getPath(selectedImageUri);
            }
            if (requestCode == SELECT_FILE2) {
                selectedPath2 = getPath(selectedImageUri);
                editText2.setText(selectedPath2);
            }
            if (requestCode == SELECT_FILE3) {
                selectedPath3 = getPath(selectedImageUri);
                editText3.setText(selectedPath3);
            }
            if (requestCode == SELECT_FILE4) {
                selectedPath4 = getPath(selectedImageUri);
                editText4.setText(selectedPath4);
            }
            editText1.setText("Selected File paths : " + selectedPath1);
        }
    }

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

我试过这段代码

Uri selectedImageUri = data.getData();
String s = getRealPathFromURI(selectedImageUri);
editText1.setText(s);

然后我得到了图像1的路径但是当我尝试第二个图像时

Uri selectedImageUri1 = data.getData();
String ss = getRealPathFromURI(selectedImageUri1);
editText2.setText(ss);

然后我得到与图像 1 相同的路径。意味着两个编辑文本对 image1 具有相同的路径。任何帮助,将不胜感激。提前致谢...

4

2 回答 2

1

((按钮) findViewById(R.id.Btn)) .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                        // in onCreate or any event where your want the user to
                        // select a file
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);
                    }
                });


 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);

                Log.v("IMAGE PATH====>>>> ",selectedImagePath);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
}
于 2013-11-14T07:39:24.470 回答
1

更改openGallery(int req_code)为:

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);
}
于 2013-11-14T07:31:30.547 回答