在我的Android应用中,我需要允许用户从手机的照片(我已经可以做)中选择多个图像然后移动(不是COPY )将当前所在位置的照片放入应用程序的特定数据目录中。
编辑:为审核添加了代码:
public class GalleryImagePicker extends Activity implements View.OnClickListener{
Context cntxt = Defines.contextGlobal;
private static final int DEFAULT_MIN_WIDTH_QUALITY = 400;
public static int minWidthQuality = DEFAULT_MIN_WIDTH_QUALITY;
private ArrayList<String> imagesPathList;
private ArrayList<Uri> imagesUri;
private LinearLayout lnrImages;
private Button btnAddPhotos;
private Button btnSaveImages;
private Bitmap yourbitmap;
private Bitmap resized;
private final int PICK_IMAGE_MULTIPLE = 1;
private ImageView targetImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryimagepicker);
lnrImages = (LinearLayout)findViewById(R.id.lnrImages);
btnAddPhotos = (Button)findViewById(R.id.btnAddPhotos);
btnAddPhotos.setOnClickListener(this);
btnSaveImages = (Button)findViewById(R.id.btnSaveImages);
btnSaveImages.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAddPhotos:
/*Intent intent = new Intent(GalleryImagePicker.this,CustomPhotoGalleryActivity.class);
startActivityForResult(intent,PICK_IMAGE_MULTIPLE);*/
Intent intent = new Intent(GalleryImagePicker.this,CustomPhotoGalleryActivity.class);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,PICK_IMAGE_MULTIPLE);
break;
case R.id.btnSaveImages:
if(imagesPathList != null)
{
if(imagesPathList.size() >= 1) {
//Toast.makeText(GalleryImagePicker.this, imagesPathList.size() + " no of images are selected", Toast.LENGTH_SHORT).show();
String[] stringArray = imagesPathList.toArray(new String[0]);
for (int i = 0; i < stringArray.length; i++){
String ThisImgPath = stringArray[i];
String PicName = ThisImgPath;
if (SystemIOFile.exists(PicName)) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
Bitmap bm = BitmapFactory.decodeFile(ThisImgPath, bmOptions);
Uri ThisUri = getImageUri(cntxt, bm);
bm = decodeBitmap(cntxt, ThisUri, bmOptions.inSampleSize);
Boolean HOLD = true;
}
Boolean HOLD = true;
}
}else{
Toast.makeText(GalleryImagePicker.this, imagesPathList.size() + " no of image are selected", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(GalleryImagePicker.this," no images are selected", Toast.LENGTH_SHORT).show();
}
break;
}
}
其他代码:
public class CustomPhotoGalleryActivity extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
/**
* Overrides methods
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages = (GridView) findViewById(R.id.grdImages);
btnSelect = (Button) findViewById(R.id.btnSelect);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
@SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
grdImages.setAdapter(imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
@Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* @param iv represented ImageView
* @param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* @author tasol
*/
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.chkImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imgThumb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId();
if (thumbnailsselection[id]) {
holder.chkImage.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imgThumb, ids[position]);
} catch (Throwable e) {
}
holder.chkImage.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* @author tasol
*/
class ViewHolder {
ImageView imgThumb;
CheckBox chkImage;
int id;
}
}
作为Android新手并使用网络发布的代码,我不得不问 上面的代码是不是已经创建了自定义UI? 如果是这样,那么如何获取原始照片URI或完整文件名以执行 COPY / DELETE ?
编辑#2:
好吧,让我继续展示我的无知......
在我的GalleryImagePicker代码中,在OnActivity中,案例为R.id.btnSaveImages:我有一个 imagesPathList()
当我输入列表中包含所选照片的“路径”的那部分代码时。
imagesPathList()内容的真实示例:
imagesPathList(0) = /storage/emulated/0/DCIM/Camera/20180419_095352.jpg
imagesPathList(1) = /storage/emulated/0/DCIM/Camera/20180419_095359.jpg
假设我设法设置了READ / WRITE权限,我不能将这些pathed文件名用于 COPY / DELETE 吗?
由于
答案 0 :(得分:0)
步骤#1:创建您自己的用户界面,允许用户选择多张照片,您要扫描文件系统以显示要显示的照片,您可以使用任何算法来确定照片的内容和不是照片。您将无法使用MediaStore
或ACTION_GET_CONTENT
或类似的内容,因为您只能使用您具有写入权限的文件。
步骤2:将minSdkVersion
设置为26(Android 8.0),然后call Files.move()
将文件从原始位置移动到所需位置。或者,如果你愿意说&#34;移动&#34;是&#34;复制,然后删除原始&#34;,您可以使用经典Java I / O复制文件代码,然后delete()
原始File
和您的{{1}无所谓。