如何在多个图像视图中设置多个图像
Posted
技术标签:
【中文标题】如何在多个图像视图中设置多个图像【英文标题】:How to set multiple image in multiple imageview 【发布时间】:2019-07-19 19:47:17 【问题描述】:我是编程和安卓新手。我正在构建一个允许用户上传多个图像的应用程序,我正在使用两个图像视图,来自相机的图片来设置一个图像视图,另一个图像视图来设置另一个相机图片来设置另一个图像和画廊中的相同图片。我需要将 2 个不同的图像上传到不同的图像视图中。如何上传?我附上了我的代码,请解决我的问题。
public class MainActivity extends AppCompatActivity
ImageView imageView1, imageView2;
private Button btn;
private static final String IMAGE_DIRECTORY = "/demonuts";
private int GALLERY = 1, CAMERA = 2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = findViewById(R.id.image1);
imageView2 = findViewById(R.id.image2);
requestMultiplePermissions();
btn = findViewById(R.id.btn);
imageView1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showPictureDialog();
);
imageView2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showPictureDialog();
);
private void showPictureDialog()
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems =
"Select photo from gallery",
"Capture photo from camera";
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
switch (which)
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
);
pictureDialog.show();
public void choosePhotoFromGallary()
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
private void takePhotoFromCamera()
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED)
return;
switch (requestCode)
case R.id.image1:
if (requestCode == GALLERY)
if (data != null)
Uri contentURI = data.getData();
try
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
String path = saveImage(bitmap);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
imageView1.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
else if (requestCode == CAMERA)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(thumbnail);
saveImage(thumbnail);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
break;
case R.id.image2:
if (requestCode == GALLERY)
if (data != null)
Uri contentURI = data.getData();
try
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
String path = saveImage(bitmap);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
imageView2.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
else if (requestCode == CAMERA)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageView2.setImageBitmap(thumbnail);
saveImage(thumbnail);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
break;
public String saveImage(Bitmap myBitmap)
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists())
wallpaperDirectory.mkdirs();
try
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]f.getPath(),
new String[]"image/jpeg", null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
return f.getAbsolutePath();
catch (IOException e1)
e1.printStackTrace();
return "";
private void requestMultiplePermissions()
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener()
@Override
public void onPermissionsChecked(MultiplePermissionsReport report)
// check if all permissions are granted
if (report.areAllPermissionsGranted())
Toast.makeText(getApplicationContext(), "All permissions are granted by user!", Toast.LENGTH_SHORT).show();
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied())
// show alert dialog navigating to Settings
//openSettingsDialog();
@Override
public void onPermissionRationaleShouldBeShown(List<com.karumi.dexter.listener.PermissionRequest> permissions, PermissionToken token)
token.continuePermissionRequest();
).
withErrorListener(new PermissionRequestErrorListener()
@Override
public void onError(DexterError error)
Toast.makeText(getApplicationContext(), "Some Error! ", Toast.LENGTH_SHORT).show();
)
.onSameThread()
.check();
【问题讨论】:
【参考方案1】:public class MainActivity extends AppCompatActivity
ImageView imageView1, imageView2;
private Button btn;
private static final String IMAGE_DIRECTORY = "/demonuts";
private int GALLERY = 1, CAMERA = 2;
**private int clickImage;**
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = findViewById(R.id.image1);
imageView2 = findViewById(R.id.image2);
requestMultiplePermissions();
btn = findViewById(R.id.btn);
imageView1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
**clickImage=1;**
showPictureDialog();
);
imageView2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
**clickImage=2;**
showPictureDialog();
);
private void showPictureDialog()
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems =
"Select photo from gallery",
"Capture photo from camera";
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
switch (which)
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
);
pictureDialog.show();
public void choosePhotoFromGallary()
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
private void takePhotoFromCamera()
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED)
return;
switch (**clickImage**)
case 1:
if (requestCode == GALLERY)
if (data != null)
Uri contentURI = data.getData();
try
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
String path = saveImage(bitmap);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
imageView1.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
else if (requestCode == CAMERA)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(thumbnail);
saveImage(thumbnail);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
break;
case 2:
if (requestCode == GALLERY)
if (data != null)
Uri contentURI = data.getData();
try
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
String path = saveImage(bitmap);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
imageView2.setImageBitmap(bitmap);
catch (IOException e)
e.printStackTrace();
Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
else if (requestCode == CAMERA)
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageView2.setImageBitmap(thumbnail);
saveImage(thumbnail);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
break;
public String saveImage(Bitmap myBitmap)
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists())
wallpaperDirectory.mkdirs();
try
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]f.getPath(),
new String[]"image/jpeg", null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
return f.getAbsolutePath();
catch (IOException e1)
e1.printStackTrace();
return "";
private void requestMultiplePermissions()
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener()
@Override
public void onPermissionsChecked(MultiplePermissionsReport report)
// check if all permissions are granted
if (report.areAllPermissionsGranted())
Toast.makeText(getApplicationContext(), "All permissions are granted by user!", Toast.LENGTH_SHORT).show();
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied())
// show alert dialog navigating to Settings
//openSettingsDialog();
@Override
public void onPermissionRationaleShouldBeShown(List<com.karumi.dexter.listener.PermissionRequest> permissions, PermissionToken token)
token.continuePermissionRequest();
).
withErrorListener(new PermissionRequestErrorListener()
@Override
public void onError(DexterError error)
Toast.makeText(getApplicationContext(), "Some Error! ", Toast.LENGTH_SHORT).show();
)
.onSameThread()
.check();
【讨论】:
以上是关于如何在多个图像视图中设置多个图像的主要内容,如果未能解决你的问题,请参考以下文章
如何为图像选择器选择多个 (2-5) 图像然后作为图像视图返回?