Android 如何从库中删除图像?

Android 如何从库中删除图像?,android,Android,这是我在文件夹mydcard文件夹“myfolder23”和移动默认图库中保存捕获图像时的源代码。如何仅在SD卡文件夹“myfolder”中保存图像而不在移动默认文件夹中?或者我如何从手机图库中删除只在SD卡“myfolder23”中保存一份图像?有什么想法吗 import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import andro

这是我在文件夹mydcard文件夹“myfolder23”和移动默认图库中保存捕获图像时的源代码。如何仅在SD卡文件夹“myfolder”中保存图像而不在移动默认文件夹中?或者我如何从手机图库中删除只在SD卡“myfolder23”中保存一份图像?有什么想法吗

import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
import android.widget.Toast;

   public class AddEditCountry extends Activity {

 private long rowID; 
 private EditText nameEt;
 private EditText capEt;
 private EditText codeEt;

 private EditText Donedate;
 private EditText Notes;
 private EditText Person;
 private  ImageView imageView1;
public  static Bitmap yourSelectedImage = null;
public static byte[] blob = null;



   private final int CAMERA_PICTURE = 1;
   private final int GALLERY_PICTURE = 2;
   private Intent pictureActionIntent = null;
   public final String SDCARD_ROOT_PATH =    
 Environment.getExternalStorageDirectory().getAbsolutePath();
public final String SAVE_PATH_IN_SDCARD = "/myFolder23/"; 
public final String IMAGE_CAPTURE_NAME    
 ="imgtemp"+System.currentTimeMillis()+".jpeg"; 

//public static        byte[] blob = new byte[2048];






   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.add_country);

      nameEt = (EditText) findViewById(R.id.Address);
      capEt = (EditText) findViewById(R.id.Stage);
      codeEt = (EditText) findViewById(R.id.Dueby);

      Donedate = (EditText) findViewById(R.id.Donedate);

      Notes = (EditText) findViewById(R.id.Notes);
      Person = (EditText) findViewById(R.id.Person);

      imageView1 = (ImageView) findViewById(R.id.imageView1);
      Button Browse = (Button) findViewById(R.id.Browse);






      Browse.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {               
        //      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        //      intent.setType("image/*");
         //     startActivityForResult(intent, 0);

                startDialog();
            }
        });        






      Bundle extras = getIntent().getExtras(); 

      if (extras != null)
      {
         rowID = extras.getLong("row_id");
         nameEt.setText(extras.getString("name"));  
         capEt.setText(extras.getString("cap"));  
         codeEt.setText(extras.getString("code"));  
         Donedate.setText(extras.getString("Location"));  
         Notes.setText(extras.getString("Notes")); 
         Person.setText(extras.getString("Person")); 



      }


      Button saveButton =(Button) findViewById(R.id.saveBtn);
      saveButton.setOnClickListener(new OnClickListener() {

          public void onClick(View v) 
          {
             if (nameEt.getText().length() != 0)
             {
                AsyncTask<Object, Object, Object> saveContactTask = 
                   new AsyncTask<Object, Object, Object>() 
                   {
                      @Override
                      protected Object doInBackground(Object... params) 
                      {
                         saveContact();
                         return null;
                      }

                      @Override
                      protected void onPostExecute(Object result) 
                      {
                         finish();
                      }
                   }; 

                saveContactTask.execute((Object[]) null); 
             }

             else
             {
                AlertDialog.Builder alert = new 
     AlertDialog.Builder(AddEditCountry.this);
                alert.setTitle(R.string.errorTitle); 
                alert.setMessage(R.string.errorMessage);
                alert.setPositiveButton(R.string.errorButton, null); 
                alert.show();
             }
          } 
     });
   }



   private void saveContact() 
   {



       ByteArrayOutputStream outStr = new ByteArrayOutputStream();
       yourSelectedImage.compress(CompressFormat.JPEG, 100, outStr);
        blob = outStr.toByteArray();



      DatabaseConnector dbConnector = new DatabaseConnector(this);

      if (getIntent().getExtras() == null)
      {
          dbConnector.insertContact(nameEt.getText().toString(),
                  capEt.getText().toString(),
                  codeEt.getText().toString(),
                  Donedate.getText().toString(),
                  Notes.getText().toString(),
                  Person.getText().toString()
                  ,blob




                  );
      }
      else
      {
         dbConnector.updateContact(rowID,
            nameEt.getText().toString(),
            capEt.getText().toString(), 
            codeEt.getText().toString(), 
            Donedate.getText().toString(),
         Notes.getText().toString(),
          Person.getText().toString()
          , blob


         );
      }
   }




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



       if (requestCode == GALLERY_PICTURE) {
            Uri uri = imageReturnedIntent.getData();
            if (uri != null) {
                // User had pick an image.
                Cursor cursor = getContentResolver().query(uri, new String[] 
  {  android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
                cursor.moveToFirst();
                final String imageFilePath = cursor.getString(0);
                File photos = new File(imageFilePath);
                yourSelectedImage = decodeFile(photos);
                yourSelectedImage = 
   Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, true);
                imageView1.setImageBitmap(yourSelectedImage);
                cursor.close();
            }
            else {
                Toast toast = Toast.makeText(this, "No Image is selected.", Toast.LENGTH_LONG);
                toast.show();
            }
        }
        else if (requestCode == CAMERA_PICTURE) {
            if (imageReturnedIntent.getExtras() != null) {
                // here is the image from camera
                yourSelectedImage = (Bitmap) 
   imageReturnedIntent.getExtras().get("data");
                imageView1.setImageBitmap(yourSelectedImage);
            }
        }




    }


   private Bitmap decodeFile(File f) {
        try {
            // decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < 
 REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, 
  o2);
        }
        catch (FileNotFoundException e) {
        }
        return null;
    }

    private void startDialog() {
        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
        myAlertDialog.setTitle("Upload Pictures Option");
        myAlertDialog.setMessage("How do you want to set your picture?");

        myAlertDialog.setPositiveButton("Gallery", new 
 DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT, 
  null);
                pictureActionIntent.setType("image/*");
                pictureActionIntent.putExtra("return-data", true);
                startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
            }
        });

        myAlertDialog.setNegativeButton("Camera", new 
    DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                pictureActionIntent = new    
 Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);











                File dir = new File(Environment.getExternalStorageDirectory() 
   + "/myFolder23");
                if(dir.exists() && dir.isDirectory()) {
                  // do something here
                 }
                else{
                    //create dir here
                    dir.mkdir(); 
                   }
                 Intent pictureActionIntent = new 
  Intent(MediaStore.ACTION_IMAGE_CAPTURE);  


                 pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
  Uri.fromFile(new
                         File(SDCARD_ROOT_PATH + 
  SAVE_PATH_IN_SDCARD,IMAGE_CAPTURE_NAME)));  




                  startActivityForResult(pictureActionIntent,CAMERA_PICTURE);  




            }
        });
        myAlertDialog.show();
    }


}
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.Toast;
公共课堂扩展活动{
私人长罗威德;
私有编辑文本名称;
私人编辑文本capEt;
私人编辑文本代码集;
私人编辑;
私人编辑文本注释;
私人编辑;
私有ImageView imageView1;
您选择的公共静态位图edimage=null;
公共静态字节[]blob=null;
私人最终int摄像机_图片=1;
私人最终int画廊图片=2;
私有意图pictureActionIntent=null;
公共最终字符串SDCARD\u根路径=
Environment.getExternalStorageDirectory().getAbsolutePath();
公共最终字符串SAVE_PATH_IN_SDCARD=“/myFolder23/”;
公共最终字符串图像\u捕获\u名称
=“imgtemp”+System.currentTimeMillis()+“.jpeg”;
//公共静态字节[]blob=新字节[2048];
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt=(EditText)findViewById(R.id.Address);
capEt=(编辑文本)findViewById(R.id.Stage);
codeEt=(EditText)findViewById(R.id.Dueby);
Donedate=(EditText)findViewById(R.id.Donedate);
Notes=(EditText)findViewById(R.id.Notes);
Person=(EditText)findViewById(R.id.Person);
imageView1=(ImageView)findViewById(R.id.imageView1);
按钮浏览=(按钮)findViewById(R.id.Browse);
Browse.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{               
//意向意向=新意向(意向.行动\u获取\u内容);
//intent.setType(“image/*”);
//StartTouchVictorResult(意向,0);
startDialog();
}
});        
Bundle extras=getIntent().getExtras();
如果(附加值!=null)
{
rowID=extras.getLong(“row_id”);
setText(extras.getString(“name”);
capEt.setText(附加getString(“cap”);
codeEt.setText(extras.getString(“代码”);
setText(extras.getString(“位置”);
Notes.setText(extras.getString(“Notes”);
Person.setText(extras.getString(“Person”);
}
按钮saveButton=(按钮)findViewById(R.id.saveBtn);
saveButton.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v)
{
if(nameEt.getText().length()!=0)
{
AsyncTask saveContactTask=
新建异步任务()
{
@凌驾
受保护对象doInBackground(对象…参数)
{
saveContact();
返回null;
}
@凌驾
受保护的void onPostExecute(对象结果)
{
完成();
}
}; 
saveContactTask.execute((Object[])null;
}
其他的
{
AlertDialog.Builder alert=新建
AlertDialog.Builder(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton,null);
alert.show();
}
} 
});
}
私有void saveContact()
{
ByteArrayOutputStream Outtr=新建ByteArrayOutputStream();
选择edimage.compress(CompressFormat.JPEG,100,outtr);
blob=outtr.toByteArray();
DatabaseConnector dbConnector=新的DatabaseConnector(此);
if(getIntent().getExtras()==null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
codeEt.getText().toString(),
Donedate.getText().toString(),
Notes.getText().toString(),
Person.getText().toString()
,斑点
);
}
其他的
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
codeEt.getText().toString(),
Donedate.getText().toString(),
Notes.getText().toString(),
Person.getText().toString()
,斑点
);
}
}
@凌驾
ActivityResult上受保护的void(int requestCode、int resultCode、Intent
图像(返回内容){
super.onActivityResult(请求代码、结果代码、图像返回内容);
if(requestCode==画廊图片){
Uri=imageReturnedIntent.getData();
if(uri!=null){
//用户选择了一个图像。
Cursor Cursor=getContentResolver().query(uri,新字符串[])
{android.provider.MediaStore.Images.ImageColumns.DATA},null,null,null);
cursor.moveToFirst();
最终字符串imageFilePath=cursor.getString(0);
文件照片=新文件(imageFilePath);
您选择的edimage=decodeFile(照片);
您选择的图像=
创建缩放位图(您选择的图像,150,150,true);
图像视图1.设置图像位图(您选择的图像);
cursor.close();
}
否则{
Toast Toast=Toast.makeText(thi