在具有android的网站上上传图像(位图)并获取网址

时间:2013-02-12 10:38:09

标签: android url bitmap

我将从我的Android代码上传图像(位图)到网站并获取网址。有没有我可以使用的简单api或我该怎么做?

这是拍摄照片并将其放入位图的主要活动:

private static final int SELECT_PHOTO = 100;
private static final int TAKE_PHOTO = 101;
static Bitmap yourSelectedImage;
ImageView im;
Uri imageUri;

/***
 * Sets up the layout an puts onClickListener() on the buttons
 * 
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);
    button = (Button) findViewById(R.id.button2);
    button.setOnClickListener(this);
}
/***
 * Change intent on the buttons you click
 */
@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.button1:
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
        break;
        case R.id.button2:
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivityForResult(intent, TAKE_PHOTO);
        break;
    }
}

/***
 * Puts the image you choosed on the bitmap that you going to paint.
 */

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

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            InputStream imageStream = null;
            try {
                imageStream = getContentResolver().openInputStream(selectedImage);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            yourSelectedImage = BitmapFactory.decodeStream(imageStream);

            Intent in = new Intent(this,EditPicsActivity.class);
            startActivity(in);

        }
        break;
    case TAKE_PHOTO:
        if (resultCode == Activity.RESULT_OK) {

            Bundle extras = imageReturnedIntent.getExtras();
            yourSelectedImage = (Bitmap) extras.get("data");

            Intent in = new Intent(this,EditPicsActivity.class);
            startActivity(in);
            break;
        }
    }
}

}

下面是对位图进行更改的代码:

public class EditPicsActivity extends Activity implements OnTouchListener {

EditView edit;
float x,y;
Paint paint = new Paint();
float radius = 10;
ArrayList<painting> whatYouEdit = new ArrayList<painting>();
Canvas canvas1;

/***
 * Sets up the surfaceview
 */

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    x=y=0;
    paint.setColor(Color.BLACK);
    edit = new EditView(this);
    edit.setOnTouchListener(this);
    setContentView(edit);
    edit.Start();
}

/***
 * Sets up the menu window
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_edit_pics, menu);

    return true;
}


/***
 * Do things when you press an option in your menu window
 */
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case R.id.Brush_Blue:
        paint.setColor(Color.BLUE);
    break;
    case R.id.Brush_Green:
        paint.setColor(Color.GREEN);
    break;
    case R.id.Brush_Red:
        paint.setColor(Color.RED);
    break;
    case R.id.Brush_Black:
        paint.setColor(Color.BLACK);
    break;
    case R.id.Brush_5:
        radius = 5;
    break;
    case R.id.Brush_10:
        radius = 10;
    break;
    case R.id.Brush_15:
        radius = 15;
    break;
    case R.id.Brush_20:
        radius = 20;
    break;
    case R.id.Save:

        Canvas c = new Canvas(MainActivity.yourSelectedImage);
        for(int i = 0; i<whatYouEdit.size();i++){
            c.drawCircle(whatYouEdit.get(i).getX() , whatYouEdit.get(i).getY(), 
                    whatYouEdit.get(i).getRadius(), whatYouEdit.get(i).getPaint());
        }

        MediaStore.Images.Media.insertImage(getContentResolver(), MainActivity.yourSelectedImage,
            "new pics", "mohahhaa");

    break;
    case R.id.Clear :
        whatYouEdit.clear();
    break;

    case R.id.Send :
        //new SendPicture().execute(MainActivity.yourSelectedImage);
        Intent sendIntent = new Intent(Intent.ACTION_SEND);
        sendIntent.setType("text/html");
        sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("http://google.se/"));
        startActivity(Intent.createChooser(sendIntent,"Share it using:"));
        break;
    }
    return false;
}


/***
 * When you touch the screen it saves the x- and y-coordinates in the painting class together with the radius
 *  and the paint color.
 */
@Override
public boolean onTouch(View v, MotionEvent event) {

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        x = event.getX();
        y = event.getY();
        whatYouEdit.add(new painting(x,y,radius,paint));
        break;
    case MotionEvent.ACTION_UP:
        x = event.getX();
        y = event.getY();
        whatYouEdit.add(new painting(x,y,radius,paint));
        break;
    case MotionEvent.ACTION_MOVE:
        x = event.getX();
        y = event.getY();
        whatYouEdit.add(new painting(x,y,radius,paint));
        break;
    }

    return true;
}

/***
 * 
 * @author Andreas Angin
 *
 */

public class EditView extends SurfaceView implements Runnable{

    Thread thread = null;
    SurfaceHolder holder;

    public EditView(Context context) {
        super(context);
        holder = getHolder();
    }


    /***
     * Paints all that you've done on the screen.
     */
    @Override
    public void run() {

        while(true){
            if(!holder.getSurface().isValid())
                continue;

            Canvas canvas = holder.lockCanvas();
            MainActivity.yourSelectedImage=getResizedBitmap(MainActivity.yourSelectedImage,this.getHeight(),this.getWidth());
            canvas.drawBitmap(MainActivity.yourSelectedImage,0,0,new Paint());
            for(int i = 0; i<whatYouEdit.size();i++){
                canvas.drawCircle(whatYouEdit.get(i).getX() , whatYouEdit.get(i).getY(), 
                        whatYouEdit.get(i).getRadius(), whatYouEdit.get(i).getPaint());
            }

            holder.unlockCanvasAndPost(canvas);
        }
    }



    private Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

        int width = bm.getWidth();
        int height = bm.getHeight();

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

        return resizedBitmap;

    }

    /***
     * starts the thread you're painting on.
     */
    public void Start() {

        thread = new Thread(this);
        thread.start();

    }
}

}

在案例R.id.Send上的onOptionsItemSelected(MenuItem item)方法中的

我想放一个链接到只有图片的页面的URL。

1 个答案:

答案 0 :(得分:0)

如果这是针对您自己的服务器,那么只需对服务器上的某个位置执行httppost(您需要一些简单的php来执行此操作(请参阅下面的链接)) - 具有已知名称(基于UUID或名称)由用户提供)在您的设备上本地存储名称并使用该名称上传,如果您的帖子有效,那么图像将在那里,您将知道您放置它的位置。

如果您不想在设备上本地存储名称,可以将名称存储在mysql数据库或该目录中的文件中,只需访问数据库或文件并检索名称列表即可是图像的名称!

用于在Android中生成UUID,只需

UUID.randomUUID()的toString(); (你可以只使用这个数字的一​​个子集)

上传图片的链接: Sending images using Http Post

http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/

http://www.coderzheaven.com/2011/04/25/android-upload-an-image-to-a-server/

http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/