get the filename (image in phone storage /download folder)

后端 未结 2 1268
迷失自我
迷失自我 2021-01-28 02:43

downloaded image shows uri shown below dat=content://com.android.providers.downloads.documents/document/22

while a get filename ==>22 is shown as filename w

相关标签:
2条回答
  • 2021-01-28 03:19

    You can get image like this:

     private void pickImage() {
          Intent pickPhotoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          pickPhotoIntent.setType("image/*");
          startActivityForResult(Intent.createChooser(pickPhotoIntent, "Select Picture"), PICK_IMAGE_ONLY);
     }
    

    You can get like this in onActivityResult() method:

      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == PICK_IMAGE_ONLY && resultCode == RESULT_OK && null != data) {
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Uri selectedMediaUri = data.getData();
                Cursor cursor = getContentResolver().query(selectedMediaUri, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                Log.e("Image Path", "Image " + " " + picturePath);
            }
        }
    

    Edit

    For DICOM Images, here i am using imbrea SDK https://github.com/binarno/Imebra-V5-Android-Simple-Dicom-Viewer

    public void loadDicomFile() {
            Intent intent = new Intent()
                    .setType("*/*")
                    .setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select a DICOM file"), PICK_DICOM);
    }
    

    OnActivtyResult method for DICOM Images:

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == PICK_DICOM && resultCode == RESULT_OK) {
                try {
                    CodecFactory.setMaximumImageSize(8000, 8000);
                    // Get the selected URI, then open an input stream
                    if (data.getData()== null) {
                        return;
                    }
                    InputStream stream = getContentResolver().openInputStream(uri);
    
                    // The usage of the Pipe allows to use also files on Google Drive or other providers
                    PipeStream imebraPipe = new PipeStream(32000);
    
                    // Launch a separate thread that read from the InputStream and pushes the data
                    // to the Pipe.
                    Thread pushThread = new Thread(new PushToImebraPipe(imebraPipe, stream));
                    pushThread.start();
    
                    // The CodecFactory will read from the Pipe which is feed by the thread launched
                    // before. We could just pass a file name to it but this would limit what we
                    // can read to only local files
                    DataSet loadDataSet = CodecFactory.load(new StreamReader(imebraPipe.getStreamInput()));
    
                    // Get the first frame from the dataset (after the proper modality transforms
                    // have been applied).
                    Image dicomImage = loadDataSet.getImageApplyModalityTransform(0);
    
                    // Use a DrawBitmap to build a stream of bytes that can be handled by the
                    // Android Bitmap class.
                    TransformsChain chain = new TransformsChain();
                    DrawBitmap drawBitmap = new DrawBitmap(chain);
                    Memory memory = drawBitmap.getBitmap(dicomImage, drawBitmapType_t.drawBitmapRGBA, 4);
    
                    // Build the Android Bitmap from the raw bytes returned by DrawBitmap.
                    Bitmap renderBitmap = Bitmap.createBitmap((int) dicomImage.getWidth(), (int) dicomImage.getHeight(), Bitmap.Config.ARGB_8888);
                    byte[] memoryByte = new byte[(int) memory.size()];
                    memory.data(memoryByte);
                    ByteBuffer byteBuffer = ByteBuffer.wrap(memoryByte);
                    renderBitmap.copyPixelsFromBuffer(byteBuffer);
    
                    // Update the image
                    binding.ivMain.setImageBitmap(renderBitmap);
                    binding.ivMain.setScaleType(ImageView.ScaleType.FIT_CENTER);
    
                } catch (IOException e) {
                    android.app.AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
                    dlgAlert.setMessage(e.getMessage());
                    dlgAlert.setTitle("Error");
                    dlgAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //dismiss the dialog
                        }
                    });
                    dlgAlert.setCancelable(true);
                    dlgAlert.create().show();
                    String test = "Test";
                }
        }
    }
    
    0 讨论(0)
  • 2021-01-28 03:36

    You can get file name using this method

    public String getName(String filename) {
        if (filename == null) {
            return null;
        }
        int index = filename.lastIndexOf('/');
        return filename.substring(index + 1);
    }
    

    In this message pass your URI as an argument

    getName(uri.toString());
    
    0 讨论(0)
提交回复
热议问题