3

画像の Uri String を文字列として SQLite に保存しました。ビューインテントを作成することで、画像を表示できます。

しかし、FileBody(new File(theUriFromTheDatabase)) を使用して Web サーバーに画像をアップロードすると、常に「open failed: ENOENT (No such file or directory)」と表示されます

ファイルの uri は「/content:/media/external/images/media/667」です。

事実:

  1. 私はそれを見ることができるので、ファイルがそこにあると確信しています
  2. 内部ストレージの読み取り/書き込み、外部ストレージの読み取り/書き込み権限を有効にしました
  3. Galaxy Tab 2 10.1 の使用
  4. それはSDカードを持っていません
  5. 同じコードは、SD カードを搭載した Experia Neo V でも動作します (SD カードがないためですか?)
  6. アプリを起動する前にワイヤーを外してみました
  7. テザー USB がオフになっています

コードは次のとおりです。

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        InspectionsDbController db = new InspectionsDbController(getActivity());

        InspectionItemStruct[] ins = db.getInspectionList(((MyApplication)((Activity) mContext).getApplication()).getCurrentInspectionId());

        SharedPreferences settings = mContext.getSharedPreferences(MainActivity.PREFS_NAME, 0);
        long userId = settings.getLong("userId", 0);
        String token = settings.getString("token", "");

        for (int i = 0; i < ins.length; i++) {


            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost("http://webprojectupdates.com/mmp/api/mobile/upload_inspection");


                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                // add userId
                try {  
                    entity.addPart("userId", new StringBody(String.valueOf(userId)));
                    entity.addPart("token", new StringBody(String.valueOf(token)));

                } catch (IOException e) {
                    Log.e("MMP","Error in adding token: "+e.getMessage());
                }




                // add media attachments
                if(ins[i].image!=null){


                    //Bitmap image = BitmapFactory.decodeFile(ins[i].image);

                    //ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    //image.compress(CompressFormat.JPEG, 75, bos);
                    //byte[] imageData = bos.toByteArray();
                    //ByteArrayBody bab = new ByteArrayBody(imageData,"image/jpg", ins[i].itemId+".jpg");

                    //entity.addPart("image", bab);
                    entity.addPart("image", new FileBody(new File (ins[i].image)));
                }
                if(ins[i].video!=null){
                    entity.addPart("video", new FileBody(new File (ins[i].video)));
                }

                // Normal string data
                try {  
                    entity.addPart("itemId", new StringBody(String.valueOf(ins[i].itemId)));
                    entity.addPart("isExist", new StringBody(String.valueOf(ins[i].itemExists)));
                    if(ins[i].comments!=null)  entity.addPart("comment", new StringBody(String.valueOf(ins[i].comments)));
                    entity.addPart("condition", new StringBody(String.valueOf(ins[i].condition)));
                } catch (IOException e) {
                    Log.e("MMP","Error in adding inspection data: "+e.getMessage());
                }

                try { 
                    httpPost.setEntity(entity);
                    HttpResponse response = httpClient.execute(httpPost, localContext);
                    String result = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    Log.e("MMP","Error in handling result: "+e.getMessage());
                }



            publishProgress(i+1,ins.length);
        }
        return null;
    }
4

1 に答える 1

3

uri 文字列の場合は、代わりにファイル名が必要でした。ありがとう@mh

ContentResolver cr = mContext.getContentResolver();
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cur = cr.query(Uri.parse(ins[i].image), projection, null, null, null);
if(cur != null) {
    cur.moveToFirst();
    String filePath = cur.getString(0);
    File imageFile = new File(filePath);
    if(imageFile.exists()) {
        // do something if it exists
        entity.addPart("image", new FileBody(imageFile));
    }
    else {
        // File was not found
        Log.e("MMP","Image not Found");
    }
} 
else {
    // content Uri was invalid or some other error occurred
    Log.e("MMP","Invalid content or some error occured");
}
于 2012-11-29T05:55:29.200 に答える