如何上传在android的多个按钮多张图片 [英] How to upload multiple images in multiple buttons in android
本文介绍了如何上传在android的多个按钮多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个按钮和两个ImageView的。当我点击第一个按钮,它会打开并上传image.How的第二个按钮点击负荷的形象做。我的code是如下:
私有静态最终诠释PICK_IMAGE = 1;
upload1 =(按钮)findViewById(R.id.uploadimage1);
upload2 =(按钮)findViewById(R.id.uploadimage2);
imgView1 =(ImageView的)findViewById(R.id.image1);
imgView2 =(ImageView的)findViewById(R.id.image2);
upload1.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){
尝试{
意向意图=新的Intent();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(意向,选择图片),
PICK_IMAGE);
}赶上(例外五){
Toast.makeText(getApplicationContext(),
无图像发现,Toast.LENGTH_LONG).show();
Log.e(e.getClass()的getName(),e.getMessage(),E。); }
}
}); @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
开关(要求code){
案例PICK_IMAGE:
如果(结果code == Activity.RESULT_OK){
乌里selectedImageUri = data.getData();
字符串文件路径= NULL; 尝试{
// OI文件管理器
串filemanagerstring = selectedImageUri.getPath(); //媒体库
字符串selectedImagePath =的getPath(selectedImageUri); 如果(selectedImagePath!= NULL){
文件路径= selectedImagePath;
}否则如果(filemanagerstring!= NULL){
文件路径= filemanagerstring;
}其他{
Toast.makeText(getApplicationContext(),未知道路,
Toast.LENGTH_LONG).show();
Log.e(位图,未知道路);
} 如果(文件路径!= NULL){
德codeFILE(文件路径);
}其他{
位= NULL;
}
}赶上(例外五){
Toast.makeText(getApplicationContext(),内部错误
Toast.LENGTH_LONG).show();
Log.e(e.getClass()的getName(),e.getMessage(),E。);
}
}
打破;
默认:
}
}
公共字符串的getPath(URI URI){
的String [] =投影{MediaStore.Images.Media.DATA};
光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
如果(指针!= NULL){
//这里你会得到一个空指针光标是否NULL
//这个可以,如果使用OI文件管理器采摘MEDIA
INT与Column_Index =光标
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}其他
返回null;
}
公共无效德codeFILE(字符串文件路径){
//德code图像尺寸
BitmapFactory.Options O =新BitmapFactory.Options();
o.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(文件路径,O); //我们希望新的大小缩放到
最终诠释REQUIRED_SIZE = 1024; //找到正确的比例值。它应该是2的幂。
INT width_tmp = o.outWidth,height_tmp = o.outHeight;
int标= 1;
而(真){
如果(width_tmp< REQUIRED_SIZE&放大器;&安培; height_tmp< REQUIRED_SIZE)
打破;
width_tmp / = 2;
height_tmp / = 2;
规模* = 2;
} //德code。与inSampleSize
BitmapFactory.Options O2 =新BitmapFactory.Options();
o2.inSampleSize =规模;
位= BitmapFactory.de codeFILE(文件路径,O2);
//第一个图像我以第一个按钮上传
imgView1.setImageBitmap(位图);
}
现在我的问题是第二个按钮如何在第二个图片上传。请帮我解决这个问题。在此先感谢
解决方案
私有静态最终诠释PICK_IMAGE = 1;
私有静态最终诠释PICK_IMAGE_2 = 2;
为 upload2
集点击监听器。然后在的OnClick()
startActivityForResult(Intent.createChooser(意向,选择图片),
PICK_IMAGE_2);
最后,在的onActivityResult()
情况下PICK_IMAGE:
imgView1.setImageBitmap(位图);
打破;
CASE PICK_IMAGE:
imgView2.setImageBitmap(位图);
打破;
I have two buttons and two imageview. When I click on first button, it opens and uploads image.How to do for second button click and load image. My code is as follows
private static final int PICK_IMAGE = 1;
upload1=(Button)findViewById(R.id.uploadimage1);
upload2=(Button)findViewById(R.id.uploadimage2);
imgView1=(ImageView)findViewById(R.id.image1);
imgView2=(ImageView)findViewById(R.id.image2);
upload1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"No image found", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = data.getData();
String filePath = null;
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
} else {
bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
break;
default:
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
//first image i have uploaded by using first button
imgView1.setImageBitmap(bitmap);
}
Now my question is for second button how to upload in second image. Please help me to solve this issue. Thanks in advance
解决方案
private static final int PICK_IMAGE = 1;
private static final int PICK_IMAGE_2 = 2;
set click listener for upload2
. Then in OnClick()
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE_2);
Finally, In onActivityResult()
case PICK_IMAGE :
imgView1.setImageBitmap(bitmap);
break;
CASE PICK_IMAGE :
imgView2.setImageBitmap(bitmap);
break;
这篇关于如何上传在android的多个按钮多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文