如何将相机图像保存到目录并压缩它

时间:2012-05-11 23:54:36

标签: android image directory

我正在开发一个应用程序,其中一个要求是在将每5个图像压缩到文件夹后将从相机捕获的图像保存到文件夹中。任何人都可以帮我修复以下代码或指出我出错的地方。欢迎任何参考资料或链接。

package c.k;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class ProjectActivity extends Activity {

Intent i;
final static int cameraData = 0;
ImageButton ib;
Bitmap bmp;
Button tg;
Button st;
private static final int CAMERA_REQUEST = 100;
protected static final int MEDIA_TYPE_IMAGE = 1;
private ImageView imageView;



/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ib = (ImageButton) this.findViewById(R.id.imageButton1);
    imageView = (ImageView) this.findViewById(R.id.imageView1);


    //trigger();
    ib.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {  
            String path = getDir("camerApp", Context.MODE_PRIVATE).getPath() + "/test.jpg";
            Log.d(path, "the path");
            File file = new File(path);
            final Uri outputFileUri = Uri.fromFile(file);
            Log.d(outputFileUri.toString(), "The file URI");
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            Log.d(MediaStore.EXTRA_OUTPUT, "The medis output");
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
   if(requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get(MediaStore.EXTRA_OUTPUT);
        Log.d("", "the bitmap image");
        imageView.setImageBitmap(photo);
    if (resultCode == RESULT_OK) {
        // Image captured and saved to fileUri specified in the Intent
        Toast.makeText(this, "Image saved to:\n" +
                 data.getData(), Toast.LENGTH_LONG).show();
    } else if (resultCode == RESULT_CANCELED) {
        // User cancelled the image capture
        Toast.makeText(this, "Image saved to:\n" +
                data.getData(), Toast.LENGTH_LONG).show();
    } } 
} 

/*
public void trigger() {
    ib = (ImageButton) findViewById(R.id.imageButton1);
    ib.setOnClickListener(this);

}

public void onClick(View v) {
    i = new Intent(android.provider.MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
    startActivityForResult(i, cameraData);
}

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

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
    }
}*/


public void onClick(View v){
    setContentView(R.layout.main);

}


public void onSettings(View V) {

    final CharSequence[] items = { "Backup Images", "Retrieve Images" };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a color");
    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item],
                            Toast.LENGTH_SHORT).show();
                }
            });
    AlertDialog alert = builder.create();

}

public FTPClient client;
public String host = "10.185.209.83";
public String username = "karthik";
public String password = "1234";
public int port = 21;

public boolean onConnect(View v) {
    try {
        client = new FTPClient();
        client.connect(host, port);

        if (FTPReply.isPositiveCompletion(client.getReplyCode())) {

            boolean status = client.login(username, password);

            client.setFileType(FTP.BINARY_FILE_TYPE);
            client.enterLocalPassiveMode();

            return status;
        }
    } catch (Exception e) {
    }

    return false;
}

// The ftpDisconnect() method is called when the client wants to disconnect
// from the server.
public boolean ftpDisconnect() {
    try {
        client.logout();
        client.disconnect();
        return true;
    } catch (Exception e) {
    }

    return false;
}

// The below code lets the user view the current working directory.
public String ftpGetCurrentWorkingDirectory() {
    try {
        String workingDir = client.printWorkingDirectory();
        return workingDir;
    } catch (Exception e) {

    }

    return null;
}

// The client can create a new directory using this method.
public boolean ftpMakeDirectory(String new_dir_path) {
    try {
        boolean status = client.makeDirectory(new_dir_path);
        return status;
    } catch (Exception e) {

    }

    return false;
}

// THe ftpDownload method lets the client download files from the server. We
// specify the path to the files and the path in the client where we want
// the files to be stored.

public boolean ftpDownload(String srcFilePath, String desFilePath) {
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);
        ;
        status = client.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {
    }

    return status;
}

// The upload method is used for sending files to the server. We specify the
// source file path where the files are found and the file name.

public boolean ftpUpload(String srcFilePath, String desFileName,
        String desDirectory) {
    boolean status = false;
    try {
        FileInputStream srcFileStream = new FileInputStream(srcFilePath);

        // change working directory to the destination directory
        if (ftpMakeDirectory(desDirectory)) {
            status = client.storeFile(desFileName, srcFileStream);
        }

        srcFileStream.close();
        return status;
    } catch (Exception e) {

    }

    return status;
}

}

0 个答案:

没有答案