2

アップロード画像(後でビデオも)Androidアプリケーションを作成しようとしています。だから私はストレージからファイル画像を取得し、imageViewに表示してから、ボタンを押すとサーバーにアップロードされます。

画像パスが存在しないという問題に出くわしましたが、その理由はわかりません。ここに私のコード:デバイスから画像を取得するボタン:

buttonUploadPhoto.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media
                    .EXTERNAL_CONTENT_URI);
            i.setType("image/*");
            startActivityForResult(i, REQUEST_IMAGE);
        }
    });

私のアクティビティ結果コード:

super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        try {
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);

            String filename = imageUri.getPath();
            destination = new File(Environment.getExternalStorageDirectory(), filename + ".jpg");
            imagePath = destination.getAbsolutePath();
            Log.e(LOG, "imagePath: " + imagePath);

            setcardPic.setImageBitmap(selectedImage);
            buttonSubmitPhoto.setVisibility(View.VISIBLE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }else {
        Toast.makeText(getApplicationContext(), "You haven't picked Image",Toast.LENGTH_LONG).show();
    }

そこから画像パスを取得しましたが、正しい名前ではありません。/storage/emulated/0/external/images/media/298.jpg のようになります。実際のファイル名は、cherry.jpg のようになります。

ここに私のアップロードボタンのコード:

public int uploadFile(String sourceFileUri) {

    String fileName = sourceFileUri;
    Log.e(LOG, "fileName :" + fileName);
    showUploadButton = false; //revert upload button to hidden
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1024 * 1024;
    File sourceFile = new File(sourceFileUri);
    Log.e(LOG, "running upload file :" + fileName);

    if (!sourceFile.isFile()) {
        dialog.dismiss();
        Log.e(LOG, "Source File not exist :" + imagePath);
        return 0;
    } else { ... (upload to server this part is working)
4

1 に答える 1