android - 如何将图像上传到Dropbox?

问题描述:

我正在使用dropbox api,但无法弄清楚如何上传图片,即使在浏览图片时,我仍然收到文件未找到异常。android - 如何将图像上传到Dropbox?

我得到这样的图像URL(我使用的浏览方法查找图片,以便图片肯定存在) 这里uri.getPath()是图片路径,看起来像这样的例子: “/外部/图像/媒体/ 536"

protected void photoToPostChosen(Uri uri){ 
    Uri photoUri = uri; 
    String id = photoUri.getLastPathSegment(); 
    if(null != id){ 
     //Bitmap thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(id), MediaStore.Images.Thumbnails.MICRO_KIND, null); 
     EditText imagetextbox = (EditText) findViewById(R.id.UI_txt_image_name); 
     imagetextbox.setText(uri.getPath()); 


    } 
} 

然后我用这个代码把它上传到Dropbox的,但它一直给我的文件未找到。

// Uploading content. 
    mySharedPreferences = getSharedPreferences("User_info_file", MODE_PRIVATE); 
    String imgpath = mySharedPreferences.getString("TEXT_IMAGELOCATION_KEY", "test"); 
    FileInputStream inputStream = null; 
    try { 
     Uri path = Uri.parse(imgpath); 
     File file = new File(path.toString()); 
     inputStream = new FileInputStream(file); 
     Entry newEntry = mDBApi.putFile("/testing.txt", inputStream, 
       file.length(), null, null); 
     Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev); 
    } catch (DropboxUnlinkedException e) { 
     // User has unlinked, ask them to link again here. 
     Log.e("DbExampleLog", "User has unlinked."); 
    } catch (DropboxException e) { 
     Log.e("DbExampleLog", "Something went wrong while uploading."); 
    } catch (FileNotFoundException e) { 
     Log.e("DbExampleLog", "File not found."); 
    } finally { 
     if (inputStream != null) { 
      try { 
       inputStream.close(); 
      } catch (IOException e) {} 
     } 
    } 

有没有人知道如果代码有问题?

+0

你最好使用'e.printStackTrace()',不是'Log.e( “DbExampleLog” 等, “找不到文件。” );',它只是告诉你什么错了,而不是根异常。 – xiaowl 2012-08-02 03:25:40

+0

我试过了,主要是说没有这样的文件或目录,我不明白其他文本行。 – omega 2012-08-02 03:32:00

+0

因此,粘贴例外以让人们知道发生了什么 – xiaowl 2012-08-02 03:52:13

没有你在XML文件中添加的权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>