Android App上传的图片非常小

时间:2013-03-15 13:45:18

标签: android upload photo

我的应用上传了已拍摄的照片。但上传的照片非常小。我怎样才能把它做大?

这是我的代码:

public void maakfoto (View v) {

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  



            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
            knop.setVisibility(Button.VISIBLE);
         // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);

            // CALL THIS METHOD TO GET THE ACTUAL PATH
            finalFile = new File(getRealPathFromURI(tempUri));

            System.out.println(finalFile);
        }  
    }

    public Uri getImageUri(Context inContext, Bitmap inImage) {
       // ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        //inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

    public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        return cursor.getString(idx); 
    }

    public void upload (View v) {

        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = finalFile.toString();
        String urlServer = "http://www.xxx.nl/testmap/upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        int serverResponseCode;
        String serverResponseMessage;

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;

        try
        {
        FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"userfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
        outputStream.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // Responses from the server (code and message)
        serverResponseCode = connection.getResponseCode();
        serverResponseMessage = connection.getResponseMessage();

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        }
        catch (Exception ex)
        {
        //Exception handling
        }

    }

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout style="@style/TitleBar">
        <ImageButton style="@style/TitleBarAction"
            android:contentDescription="@string/description_home"
            android:src="@drawable/title_home"
            android:onClick="onClickHome" />

        <ImageView style="@style/TitleBarSeparator" />

        <TextView
            style="@style/TitleBarText"
            android:text="Foto uploaden" />

    </LinearLayout>

    <ImageView
        android:id="@+id/fotoVenster"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_gravity="center_horizontal" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Maak foto"
        android:layout_gravity="bottom"
        android:onClick="maakfoto" />    

    <Button
        android:id="@+id/uploadknop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:onClick="upload"
        android:text="Upload de bovenstaande foto" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>

</LinearLayout>

如何让上传的照片更大?现在他们是最大的。 250 x 350像素。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为您使用intent返回的位图设置ImageView,默认情况下是缩略图。解决方案是将捕获的图像保存到手机上的某个位置,然后从此位置读取。继承人如何:

private void takePhoto()
    {
        Intent takePic = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        takePic.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)));
        startActivityForResult(takePic, CAMERA_REQUEST);
    }

创建位置的方法:

private File getTempFile(Context context)
    {
        // it will return /sdcard/image.tmp
        final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName());
        if (!path.exists())
        {
            path.mkdir();
        }
        return new File(path, "image.tmp");
    }

不是从意图中获取图像,而是从保存的临时图像中检索图像:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_REQUEST)
        {

            final File file = getTempFile(this);
            try
            {
                Uri selectedImage;
                String pathToMediaFile = MediaStore.Images.Media.insertImage(getContentResolver(), Uri.fromFile(file).getPath(), "", "Image taken via my app");
                selectedImage = Uri.parse(pathToMediaFile);

                String[] filePathColumn =
                { MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

                String filePath = cursor.getString(columnIndex);
                cursor.close();

                ImageCacheManager icm = new ImageCacheManager(this);
                int scale = decodeFile(filePath);
                Drawable dd = icm.loadLocalImage(filePath, scale);

                imageView.setImageDrawable(dd);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

    }

然后,您可以像平常一样上传此图片,它将是完整尺寸。

如果有帮助,请告诉我。

相关问题