2014-05-12 4 views
0

android 앱에서 /MNT/SDCARD 또는 ASSETS/FILES 폴더의 사진을 보내고 싶습니다.서버에 파일로드 및 저장 HTTP POST 요청

05-12 16:15:15.468: E/AndroidRuntime(17228): Caused by: java.io.FileNotFoundException: /data/data/com.example.app_flower3/files/qqqwww.png (No such file or directory) 

05-12 16:15:15.468: E/AndroidRuntime(17228): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 


05-12 16:39:20.417: E/AndroidRuntime(19575): Caused by: java.lang.IllegalArgumentException: File //mnt//sdcard//gauss.jpg contains a path separator 

이 내 코드입니다 :

public Map<String,String> postSendImageAndGetJson() throws ParseException, FileNotFoundException 
{ 
    File rootPath=Environment.getExternalStorageDirectory(); 
    Log.d("CILO",rootPath.getPath().toString()); 

    printer.log("CHESTIE!!"); 
    // Create map 
    Map<String,String> map = new TreeMap<String,String>(); 


    BufferedInputStream buf = new BufferedInputStream(context.openFileInput("//mnt//sdcard//gauss.jpg")); 
    Bitmap bitmapOrg = BitmapFactory.decodeStream(buf); 

    ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

    String upload_url = "http://www.aplicatii-iphone.ro/android/test.php"; // HARDCODED 
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); 

    byte[] data = bao.toByteArray(); 

    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost postRequest = new HttpPost(upload_url); // HARDCODED 
    MultipartEntity entity = new MultipartEntity(
    HttpMultipartMode.BROWSER_COMPATIBLE); 

    //Set Data and Content-type header for the image 
    entity.addPart("file", 
    new ByteArrayBody(data, "image/jpeg", "file")); 
    postRequest.setEntity(entity); 
    try 
    { 
     HttpResponse response = httpClient.execute(postRequest); 
     //Read the response 
     String jsonString = EntityUtils.toString(response.getEntity()); 
     Log.d("CILO", "after uploading file "+ jsonString); 

    } 
    catch (ClientProtocolException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return map; 
} 
  1. 내가 실수를 않습니다 나는 오류의 그 종류는 무엇입니까?
  2. 인터넷에서 검색 한 결과, 안드로이드 앱 (이미지)에 대한 데이터 작업에 대한 유용한 자습서를 어디에서 찾을 수 있습니까?

감사합니다.

답변

0

이 줄에는 다음과 같은 문제가 있습니다. BufferedInputStream buf = new BufferedInputStream(context.openFileInput("//mnt//sdcard//gauss.jpg")); 파일을 여는이 방법을 사용하면 응용 프로그램 만 액세스 할 수있는 응용 프로그램 용으로 만든 디렉토리 인 안드로이드의 내부 파일 저장소가 사용됩니다. SD 카드에 저장된 파일을 열려면 다음과 같이 사용하십시오.

File dirSD = new File(Environment.getExternalStorageDirectory(), "folder_name"); 
File sdFile = new File(dirSD , "somefilename.txt"); 
try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String content = ""; 
    String buffer; 
    while ((buffer = br.readLine()) != null) { 
     content += buffer + "\n" 
    } 
    br.close(); 
} 

이렇게하면 텍스트 파일로 변환 할 수 있습니다. 이미지를 다루는 것처럼 보이므로 다른 방식 (예 : 문자열을 사용하여 파일의 내용을 저장하지 않음)으로 이미지를 다룰 수 있습니다. 거기에 확실히 example code already on stack overflow

+0

'BufferedInputStream buf = 새로운 BufferedInputStream (새 InputStream (sdFile));'이 오류가 발생합니다,'BufferedInputStream' 생성자는'InputStream' 객체 만 받아들이십시오. 완전한 코드가 있습니까? 고맙습니다. – Master345

+0

내가 만든 편집을 확인하고, 잘하면 그것을 정리해야합니다. 행운을 빕니다 – bkane521