2013-10-13 78 views
1

我對Android非常陌生。我正在爲我的項目構建一個繪圖應用程序,我想知道你們是否可以就我的問題提供建議。這是我的代碼Android:是否可以在服務器內的文件夾上上傳圖片?

這是我paintclass

 

    public class paintclass extends View{ 



    //InputStream is = getResources().openRawResource(R.drawable.pic); 

    Bitmap bitmap; 
    private Canvas mcanvas; 
    static Paint paint; 
    static Path path = new Path(); 
    //private Paint cpaint; 
    //private Path cpath = new Path(); 
    static int[] color = {255, 0, 0, 0 }; 
    static int[] stroke = {5, 20}; 
    static boolean eraser = false; 
    public LayoutParams params; 
    LinearLayout parentLinearLayout; 
    LinearLayout parentLinearLayout2; 
    LinearLayout ll; 



    public paintclass(Context context){ 
    super(context); 


    bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); 
    mcanvas = new Canvas(bitmap); 
    } 

    public paintclass(Context context, AttributeSet a){ 
    super(context, a); 
    bitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888); 
    mcanvas = new Canvas(bitmap); 
    } 

    @Override protected void onDraw(Canvas canvas){ 
    super.onDraw(canvas); 
    paint = new Paint(); 
    paint.setColor(Color.RED); 
    paint.setAntiAlias(true); 
    paint.setARGB(color[0],color[1], color[2], color[3]); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeJoin(Paint.Join.ROUND); 
    if(eraser == true){paint.setStrokeWidth(stroke[1]);} 
    else{paint.setStrokeWidth(stroke[0]);} 

    paint.setDither(true); 
    paint.setStrokeCap(Paint.Cap.ROUND); 
     canvas.drawColor(Color.WHITE); 
    canvas.drawBitmap(bitmap, 0, 0, paint); 
    canvas.drawPath(path, paint); 

     invalidate(); 
    } 




    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

    private void touch_start(float x, float y) { 
    path.reset(); 
    path.moveTo(x, y); 
    mX = x; 
    mY = y; 
    } 
    private void touch_move(float x, float y) { 

    float dx = Math.abs(x - mX); 
    float dy = Math.abs(y - mY); 
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
     path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
     mX = x; 
     mY = y; 
    } 
    } 
    private void touch_up() { 
    path.lineTo(mX, mY); 
    mcanvas.drawPath(path, paint); 
    path.reset(); 
    } 



    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
    // TODO Auto-generated method stub 
    // super.onTouchEvent(event); 

    float a = event.getX(); 
    float b = event.getY(); 

    switch (event.getAction()){ 
    case MotionEvent.ACTION_DOWN: 
      touch_start(a, b); 
      invalidate(); 
      break; 
    case MotionEvent.ACTION_MOVE: 
     touch_move(a, b); 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_UP: 
      touch_up(); 
      invalidate(); 
      break; 
    } 
    return true; 
    } 


    } 





和我的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/liner" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#ffffcc" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ff9900" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/prima" 
     android:layout_width="47dp" 
     android:layout_height="47dp" 
     android:src="@drawable/pic" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="44dp" 
     android:layout_marginLeft="10dp" 
     android:singleLine="false" 
     android:text="Temporary text" 
     android:textAppearance="?android:attr/textAppearanceSearchResultTitle" 
     android:textSize="13sp" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffcc" 
    android:orientation="horizontal" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp" > 

    <ImageButton 
     android:id="@+id/bl" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/bl" /> 

    <ImageButton 
     android:id="@+id/r" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/r" /> 

    <ImageButton 
     android:id="@+id/b" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/b" /> 

    <ImageButton 
     android:id="@+id/y" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/y" /> 

    <ImageButton 
     android:id="@+id/o" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/o" /> 

    <ImageButton 
     android:id="@+id/g" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/g" /> 

    <ImageButton 
     android:id="@+id/p" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/p" /> 

    <ImageButton 
     android:id="@+id/ad" 
     android:layout_width="36dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ad" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
      android:layout_weight="50" 
    android:background="#cccccc" 
    android:orientation="vertical" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 

    > 

    <LinearLayout 
     android:id="@+id/drawingAre" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:background="#ffffff" 
     android:orientation="vertical" > 

     <com.depictry.paintclass 
      xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/draw" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/addsize" 
     style="?android:attr/buttonStyleInset" 
     android:layout_width="45dp" 
     android:layout_height="45dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginTop="5dp" 
     android:text="+" /> 

    <Button 
     android:id="@+id/minussize" 
     style="?android:attr/buttonStyleInset" 
     android:layout_width="45dp" 
     android:layout_height="45dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginTop="5dp" 
     android:text="-" /> 

    <Button 
     android:id="@+id/clear" 
     style="?android:attr/buttonStyleInset" 
     android:layout_width="wrap_content" 
     android:layout_height="45dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     android:layout_marginTop="5dp" 
     android:layout_weight="0.10" 
     android:text="Clear" /> 

    <Button 
     android:id="@+id/saveimage" 
     style="?android:attr/buttonStyleInset" 
     android:layout_width="wrap_content" 
     android:layout_height="45dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     android:layout_marginTop="5dp" 
     android:layout_weight="0.08" 
     android:text="Send" /> 

</LinearLayout> 

</LinearLayout> 

,我的主要活動。

 

     but[i] = (Button)findViewById(R.id.saveimage); 
       but[i].setOnClickListener(new View.OnClickListener() { 
        @Override public void onClick(View v) { 


    LinearLayout ll = (LinearLayout) findViewById(R.id.drawingAre); 
    ImageView im = (ImageView) findViewById(R.id.prima); 

    ll.setDrawingCacheEnabled(true); 
    ll.buildDrawingCache(true); 
    Bitmap b = Bitmap.createBitmap(ll.getDrawingCache()); 
    ll.setDrawingCacheEnabled(false); 
    im.setImageBitmap(b); 

這個代碼就在這裏,我想我的圖像上的服務器文件夾上傳,但它的作用是顯示圖像。任何人都可以幫忙嗎?

+0

你想讓它顯示然後上傳嗎?或只上傳? –

+0

服務器如何接受文件? FTP? HTTP? SFTP? POST/GET?這些都可以做,但每個都需要不同的路徑。 – PearsonArtPhoto

回答

0

是的,你可以上傳圖像到服務器內的文件夾。
This答案可以幫到你。
其uploadToServer類將對您有用,只需使用路徑到服務器上的文件夾即可。

相關問題