2017-04-13 3 views
-1

나는 안드로이드에서 Azure Blob 스토리지에 이미지를 업로드하려고합니다. 나는이 방법andriod의 푸른 blob 스토리지

CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString); 

     // Create the blob client. 
     CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); 

     CloudBlobContainer container = blobClient.getContainerReference("mycontainer"); 

     final String filePath = "C:\\Users\\icon.jpg"; 

     CloudBlockBlob blob = container.getBlockBlobReference("1.jpg"); 
     File source = new File(filePath); 
     blob.upload(new FileInputStream(source), source.length()); 

으로 자바에서 그것을 할 수 있지만 내가 파일 경로를 변경하는 경우에 "내용 : // 미디어/외부/이미지/미디어/12"이, 안드로이드에 내가 FileNotFoundException이 있습니다. 어떻게하면 안드로이드에서 이미지를 업로드 할 수 있습니까?

+0

처럼? –

답변

0
  CloudBlockBlob blob = container.getBlockBlobReference(UserInfo.username + ".jpg"); 
      BlobOutputStream blobOutputStream = blob.openOutputStream(); 
      ContentResolver cr = context.getContentResolver(); 
      InputStream s = cr.openInputStream(uri); 
      byte[] arr = convertInputStreamToByteArray(s); 
      ByteArrayInputStream inputStream = new ByteArrayInputStream(arr); 
      int next = inputStream.read(); 
      while (next != -1) { 
       blobOutputStream.write(next); 
       next = inputStream.read(); 
      } 
      blobOutputStream.close(); 

스트림으로 변환하면 코드에 지정된 경로에 파일이 있음을

public byte[] convertInputStreamToByteArray(InputStream inputStream) 
    { 
     byte[] bytes= null; 

     try 
     { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

      byte data[] = new byte[1024]; 
      int count; 

      while ((count = inputStream.read(data)) != -1) 
      { 
       bos.write(data, 0, count); 
      } 

      bos.flush(); 
      bos.close(); 
      inputStream.close(); 

      bytes = bos.toByteArray(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return bytes; 
    } 
+0

감사합니다. 작동 중입니다.)) –

1

최종 문자열적인 filePath = "C : \ 사용자 \ icon.jpg"

이 당신의 장치에 존재하는 파일을 가리키는 I 거의 의심의 여지.

편집하지만 콘텐츠에 파일 경로를 변경하는 경우

: // 미디어/외부/이미지/미디어/12

이 경로를 파일이 아닙니다. content:// Uri를 사용하려면 ContentResolver와 openInputStream()openOutputStream()과 같은 메서드를 사용해야합니다.

+0

미안하지만, 제 질문을 편집했습니다 –

+0

편집 된 답변보기 –

+0

내가 이렇게해야합니까? CloudBlockBlob blob = container.getBlockBlobReference (UserInfo.username + ".jpg"); ContentResolver cr = context.getContentResolver(); InputStream s = cr.openInputStream (uri); ByteArrayOutputStream os = new ByteArrayOutputStream(); int b; while ((b = s.read())! = -1) os.write (b); blob.upload (s, os.size()); –

관련 문제