我是将图像(位图)从我的 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。