Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

file - Get uri from image in Android

I am trying to get the Path from an image to send it later to a server. The problem is when I try to get it, my code doesn't work (you will see there is an extra }. That is because the code from the OnCreate ends and then I worte the other functions):

enviar.setOnClickListener(new View.OnClickListener() {
            String datos="";
            //Bundle extras=getIntent().getExtras();
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
                //Uri imagen=intent.getData();
                //datos=imagen.getPath();
                //mostrar.setText(datos);
            }
        });
    }

    private String getRealPathFromURI(Uri contentURI) {
        String result;
        Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) {
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch(requestCode) {
            case 0:
                if(resultCode == this.RESULT_OK){
                    try {
                        final Uri imageUri = imageReturnedIntent.getData();
                        String path = getRealPathFromURI(imageUri);
                        mostrar.setText(path);
                    }
                    catch (Exception e) {
                        Log.e("Erroreeeee: ", e.getMessage());
                    }
                }
                break;
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two problems.

The first one is that startActivityForResult() is not immediate. You do not have any results in the next statement.

So, you are welcome to call startActivityForResult(), as I do in this sample app:

private void get() {
    Intent i=
      new Intent()
        .setType("image/png")
        .setAction(Intent.ACTION_GET_CONTENT)
        .addCategory(Intent.CATEGORY_OPENABLE);

    startActivityForResult(i, REQUEST_GET);
}

Your results are delivered to onActivityResult():

  @Override
  public void onActivityResult(int requestCode, int resultCode,
                               Intent resultData) {
    if (resultCode==Activity.RESULT_OK) {
      Uri image=resultData.getData();
      // do something
    }
  }

Your second problem is that you are thinking that you are picking a file. You are not. You are picking a piece of content, using any activity that the user decides to have handle ACTION_GET_CONTENT. getPath() only has meaning if the scheme of the Uri is file, and that is rather unlikely. There is no reliable means of getting a filesystem path for an arbitrary Uri, for the simple reason that the Uri does not have to point to a file.

Ideally, your "upload to a server" logic can work with an InputStream. In that case, call openInputStream() on a ContentResolver to get an InputStream on the content identified by the Uri. If your "upload to a server" logic only works with files, use that InputStream to copy the content to some temporary file that you control (e.g., in getCacheDir()), then use that temporary file for your upload. Delete the temporary file when you are done with it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...