2013-11-29 102 views
0

我遇到問題,特別是將路徑UriKITKAT轉換爲Base64格式。該代碼適用於API 18及更低版本的所有設備。有人可以請幫助。轉換文件路徑時出錯

11-29 09:57:44.407: I/FILE_URI(23833): content://com.android.providers.media.documents/document/image%3A6108 

這是從路徑烏里返回的內容。

以下爲Base64編碼的轉換代碼:

public void getGalleryDetails(String path) throws FileNotFoundException { 


    InputStream inputStream = new FileInputStream(path); 
    byte[] bytes; 
    byte[] buffer = new byte[8192]; 
    int bytesRead; 
    ByteArrayOutputStream output = new ByteArrayOutputStream(); 
    try{ 
     while((bytesRead = inputStream.read(buffer)) != -1){ 
      output.write(buffer, 0, bytesRead); 
     } 
    }catch(IOException e){ 
     e.printStackTrace(); 
    } 

    bytes = output.toByteArray(); 

    encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT); 
    Log.i("ENCODED", encodedImage); 
} 

回答

0

因爲KITKAT有處理的文檔的一些其他的機制,現在,我用了以下內容:

if (Build.VERSION.SDK_INT < 19) { 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        startActivityForResult(
          Intent.createChooser(intent, "Choose Picture"), 
          FROM_GALLERY); 
       } else { 
        Intent intent = new Intent(
          Intent.ACTION_PICK, 
          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, FROM_GALLERY); 
       }