2

我是将图像(位图)从我的 android 代码上传到网站并获取 url 的内容。有没有我可以使用的简单 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。

4

1 回答 1

0

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

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

在android中生成一个UUID很简单

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

上传图片的链接: 使用 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/

于 2013-02-12T13:34:57.587 回答