2017-08-31 86 views
-2

我想獲取圖像路徑並上傳到服務器。 在這裏,我成功地從圖庫中讀取圖像並設置爲圖像視圖,但圖像路徑返回null。Android中的圖像路徑返回null

public void onActivityResult(int requestCode, int resultCode, Intent data) 
      if (resultCode == RESULT_OK) { 
       if (requestCode == SELECT_PICTURE) { 
        // Get the url from data 
        Uri selectedImageUri = data.getData(); 
        if (null != selectedImageUri) { 
         // Get the path from the Uri 
         String path = getRealPathFromURI(getActivity(), selectedImageUri); 
         Log.i(TAG, "IMAGE" + path); 
         Log.d("INFO", selectedImageUri.toString()); 
         // Set the image in ImageView 
         profilepicture.setImageURI(selectedImageUri); 
        } 
       } 
      } 
     } 

     /* Get the real path from the URI */ 
     public String getRealPathFromURI(Context context, Uri contentUri) { 
      Cursor cursor = null; 
      try { 
       String[] proj = {MediaStore.Images.Media.DATA}; 
       cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToFirst(); 
       return cursor.getString(column_index); 
      } finally { 
       if (cursor != null) { 
        cursor.close(); 
       } 
      } 
     } 
+0

'形象Android'路徑返回null。一點都不。錯誤的主題。 getRealPathFromURI()返回null。 – greenapps

+0

'想要獲取圖像路徑'。從何而來?你沒有告訴你從哪裏得到'形象'。或者那個意圖。 – greenapps

回答

-1

試試這個

String path = yourAndroidURI.toString() // "/mnt/sdcard/FileName.mp3" 
File file = new File(new URI(path)); 
+0

感謝您使用此代碼段,這可能會提供一些即時幫助。通過展示*爲什麼*這是一個很好的解決方案,對未來的讀者會有更好的解決方案,這將爲它的教育價值提供一個合適的解釋[//大大提高](// meta.stackexchange.com/q/114762)但不完全相同的問題。請編輯您的答案以添加解釋,並指出適用的限制和假設。 –

-1

使用此功能將字符串轉換網址爲位圖

public Bitmap getImage(String url) { 
     try { 
      BufferedInputStream bis = new BufferedInputStream(new 
      URL(url).openStream(), BUFFER_IO_SIZE); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      BufferedOutputStream bos = new BufferedOutputStream(baos, 
      BUFFER_IO_SIZE); 
      copy(bis, bos); 
      bos.flush(); 
      return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, 
      baos.size()); 
     } catch (IOException e) { 
      Log.d(TAG, "loadImageFromArrayList: IMAGE DOWNLOAD FAILED!" +e); 
     } 
     return null; 
    } 
+0

它會在您傳遞URL時回覆位圖 –

0

刪除getRealPathFromURI(),因爲它是不會可靠地工作。

取而代之,請使用您最喜歡的圖像加載庫(例如Picasso,Glide)從Uri加載圖像。

或者,在最壞的情況下,使用getContentResolver().openInputStream()獲得由Uri標識內容的InputStream,那麼流傳遞到BitmapFactory.decodeStream()。只需在後臺線程上執行此I/O操作(其中包括哪些映像加載庫將爲您處理,以及其他優點)。

0

更新你的方法getRealPathFromURI:

public String getRealPathFromURI(Context context, Uri contentUri) { 
     Cursor cursor = null; 
     try { 
      String[] proj = {MediaStore.Images.Media.DATA}; 
      cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      String imagePath = cursor.getString(column_index); 
      if (imagePath == null) { 
       File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
         Environment.DIRECTORY_PICTURES), "New"); 
       if (!mediaStorageDir.exists()) { 
        if (!mediaStorageDir.mkdirs()) { 
         Log.d("MyCameraApp", "failed to create directory"); 
         return null; 
        } 
       } 
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
       File file; 
       String path = "img_" + timeStamp + ".jpg"; 
       file = new File(mediaStorageDir.getPath() + File.separator + path); 

       imagePath = file.getAbsolutePath(); 
       ParcelFileDescriptor parcelFileDescriptor = null; 
       try { 
        parcelFileDescriptor = context.getContentResolver() 
          .openFileDescriptor(contentUri, "r"); 
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); 
        Bitmap image = BitmapFactory 
          .decodeFileDescriptor(fileDescriptor); 
        parcelFileDescriptor.close(); 
        saveBitmapToPath(image, imagePath); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      return imagePath; 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
    }