将位图转换为图像文件时未创建目录

时间:2015-04-05 00:58:27

标签: android directory

这是我的班级。在这里,我让用户在油漆上画一些东西。然后我想在用户点击菜单栏中的保存选项时将图像保存为图像文件。但是使用此代码,不会创建目录。

package com.nisani.sampaint;

 import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
 import android.graphics.Bitmap.Config;
 import android.graphics.Canvas;
 import android.graphics.Color;
  import android.graphics.Matrix;
   import android.graphics.Paint;
  import android.graphics.PointF;
   import android.graphics.drawable.BitmapDrawable;
     import android.graphics.drawable.Drawable;

   import android.os.Bundle;
   import android.os.Environment;
   import android.util.Log;

  import android.view.Display;
  import android.view.Menu;
 import android.view.MenuItem;
  import android.view.MotionEvent;
  import android.view.View;
    import android.view.View.OnTouchListener;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import android.widget.Toast;

    import java.io.File;
    import java.io.FileOutputStream;

  public class MainActivity extends Activity implements OnTouchListener {
  ImageView imageView;

  Bitmap bitmap;
  Canvas canvas;
static int mImageCount;
Intent in;
  Paint paint;
  float downx = 0, downy = 0, upx = 0, upy = 0;
  float startX = 0;
    float startY = 0;
    float endX = 0;
    float endY = 0;
View mView;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

      Toast.makeText(getApplicationContext(), "Rotate the phone to clear screen!!",Toast.LENGTH_SHORT ).show();
    imageView = (ImageView) this.findViewById(R.id.imageView1);  
    Display currentDisplay = getWindowManager().getDefaultDisplay();
    float dw = currentDisplay.getWidth();
    float dh = currentDisplay.getHeight();   
    bitmap = Bitmap.createBitmap((int) dw, (int) dh,
        Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    paint = new Paint();
    paint.setColor(Color.BLACK);
    imageView.setImageBitmap(bitmap);

    imageView.setOnTouchListener(this);
  }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_pattern1, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_home) {
        return true;
    }
    if (id == R.id.action_p1) {
        in=new Intent(MainActivity.this,Pattern1.class);
        startActivity(in);
    }
    if (id == R.id.action_rect) {
        in=new Intent(MainActivity.this,Rectangle.class);
        startActivity(in);
    }
    if (id == R.id.action_save) {


        File myDir=new File("/sdcard/sampaint");
        boolean a=myDir.mkdirs();
        if(a==true){
            Toast.makeText(MainActivity.this,"directory created",Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(MainActivity.this,"directory not created",Toast.LENGTH_SHORT).show();
        }

        mImageCount++;
        String fname = "image-" + mImageCount + ".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        saveAsJpg (file);
    }
    return super.onOptionsItemSelected(item);
}
public void saveAsJpg (File f) {
    String fname = f.getAbsolutePath ();
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream (f);
// Take the bitmap of the view and write it out as a jpeg.
        bitmap.compress (Bitmap.CompressFormat.JPEG, 95, fos);
    } catch (Throwable ex) {
       ex.printStackTrace();
    }

}
public void onBackPressed(){

    finish();
    super.onBackPressed();
}
 public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();

    switch (action) {       
    case MotionEvent.ACTION_DOWN:
        startX=event.getX();
        startY=event.getY();
        break;
    case MotionEvent.ACTION_MOVE:           
    endX = event.getX();
    endY = event.getY();
   canvas.drawLine(startX,startY,endX,endY, paint);

    imageView.invalidate();
    startX=endX;
    startY=endY;
    break;
    case MotionEvent.ACTION_UP:
        endX = event.getX();
        endY = event.getY();
        canvas.drawLine(startX,startY,endX,endY, paint);
        //canvas.drawPaint(paint);
        imageView.invalidate();
    break;
    case MotionEvent.ACTION_CANCEL:
    break;
    default:
    break;
    }
    return true;
  }
}

1 个答案:

答案 0 :(得分:0)

将路径指定为Environment.getExternalStorageDirectory()+" / sampaint /" + filename.jpg

执行以下操作 文件文件=新文件(Environment.getExternalStorageDirectory()+" / sampaint /",fname);