0

画像をアップロードする方法に関するチュートリアルに従いましたが、ファイルが表示されず、どこで間違ったのかわかりません。基本的にスマホのギャラリーから写真を選んで送っています。

以下は私のコードです:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    btnUpload = (Button) this.findViewById(R.id.btnUpload);

    btnUpload.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            // To open up a gallery browser
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"), 1);
        }
    });

    btnSent = (Button) this.findViewById(R.id.btnSent);

    btnSent.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            HttpUploader uploader = new HttpUploader();
            try {
                // String image_name =
                uploader.execute(getRealPathFromURI(currImageURI)).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    });
}

// To handle when an image is selected from the browser
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            // currImageURI is the global variable I’m using to hold the
            // content:
            currImageURI = data.getData();
            path = (TextView) findViewById(R.id.path);
            path.setText(getRealPathFromURI(currImageURI));
        }
    }
}

// Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
    android.database.Cursor cursor = managedQuery(contentUri, proj, // Which
                                                                    // columns
                                                                    // to
                                                                    // return
            null, // WHERE clause; which rows to return (all rows)
            null, // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

これが私のhttpです:

for (String sdPath : path) {
        Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        // Resize the image
        double width = bitmapOrg.getWidth();
        double height = bitmapOrg.getHeight();
        double ratio = 400 / width;
        int newheight = (int) (ratio * height);

        bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight,
                true);

        // Here you can define .PNG as well
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        byte[] ba = bao.toByteArray();
        String ba1 = Base64.encodeBytes(ba);

        System.out.println("uploading image now ——–" + ba1);

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("image", ba1));

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://path to my server/upload.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            // print response
            outPut = EntityUtils.toString(entity);
            Log.i("GET RESPONSE—-", outPut);

            // is = entity.getContent();
            Log.e("log_tag ******", "good connection");

            bitmapOrg.recycle();

        } catch (Exception e) {
            Log.e("log_tag ******",
                    "Error in http connection " + e.toString());
        }

これは私のphpコードです:

<?php

$base = $_REQUEST["image"];

if (isset($base)) {

$suffix = createRandomID();
$image_name = "img_".$suffix."_".date("Y-m-d-H-m-s").".jpg";

// base64 encoded utf-8 string
$binary = base64_decode($base);

// binary, utf-8 bytes

header("Content-Type: bitmap; charset=utf-8");

$file = fopen("../images/post_images/" . $image_name, "wb");

fwrite($file, $binary);

fclose($file);

die($image_name);

} else {

die("No POST");
}

function createRandomID() {

$chars = "abcdefghijkmnopqrstuvwxyz0123456789?";
//srand((double) microtime() * 1000000);

$i = 0;

$pass = "";

while ($i <= 5) {

$num = rand() % 33;

$tmp = substr($chars, $num, 1);

$pass = $pass . $tmp;

$i++;
}
return $pass;
}
?>
4

1 に答える 1

0

このスクリプトは機能しており、servarのアップロードフォルダーに画像があります。

<?php 
 $target = "upload/"; 

 $target = $target . basename( $_FILES['uploaded']['name']) ; 
 $ok=1; 
 if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
 {
     echo "yes";
 } 
 else {
     echo "no";
 }
?> 

この答えを見てください

于 2012-09-25T09:01:31.663 に答える