从画廊或相机上传图像到服务器 android
Upload image to server from gallary or camera android
我有一个 Activity 用于从图库或相机上传图像到 server.image 从相机上传没问题,但从图库上传不是 done.i 有错误显示
BitmapFactory﹕ Unable to decode stream: FileNotFoundException
我想做的是当我从图库中拾取图像时它显示在图像视图的其他 Activity 中。
我不知道如何获取 gallary 的 fileuri 请帮助我。
我的代码:
loadimage = (ImageView) findViewById(R.id.profilpic);
loadimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
/* Intent i = new Intent(Tab1.this, ImageMain.class);
startActivity(i);*/
// selectImageFromGallery();
AlertDialog.Builder builder = new AlertDialog.Builder(Tab1.this);
builder.setMessage("Select Image From")
.setCancelable(true)
.setPositiveButton("camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_REQUEST);
}
})
.setNegativeButton("gallary", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
听说是我的onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
launchUploadActivity(true);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
else if(requestCode==RESULT_LOAD_IMAGE){
if (resultCode==RESULT_OK){
launchUploadActivity(true);
}
}
}
private void launchUploadActivity(boolean isImage){
Intent i = new Intent(Tab1.this,UploadAvtivity.class);
i.putExtra("filePath", fileUri.getPath());
i.putExtra("isImage", isImage);
startActivity(i);
}
这是通过默认相机拍照的代码片段(这里我实现了 Intent 来获取图像)。之后将其存储到SD卡(此处将创建一个新文件并存储新拍摄的图像);如果您不想存储,则从代码中删除保存部分。之后,您可以将文件路径用于上传目的。然后您可以参考它并更改以获取您想要的路径。
在 class 区域放置这些行
final int TAKE_PHOTO_REQ = 100;
String file_path = Environment.getExternalStorageDirectory()
+ "/recent.jpg";//Here recent.jpg is your image name which will going to take
之后通过将这些行放在调用方法中来调用相机。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQ);
然后在你的 Activity 中添加这个方法来获取图片并将其保存到 sd 卡,你可以从这里调用你的上传方法,通过知道它的路径上传图片。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO_REQ: {
if (resultCode == TakePicture.RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
// ... (process image if necesary)
imageView.setImageBitmap(srcBmp);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
srcBmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(file_path);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// remember close de FileOutput
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("take-img", "Image Saved to sd card...");
// Toast.makeText(getApplicationContext(),
// "Image Saved to sd card...", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
希望这对您和其他人也有帮助..谢谢
我有一个 Activity 用于从图库或相机上传图像到 server.image 从相机上传没问题,但从图库上传不是 done.i 有错误显示
BitmapFactory﹕ Unable to decode stream: FileNotFoundException
我想做的是当我从图库中拾取图像时它显示在图像视图的其他 Activity 中。 我不知道如何获取 gallary 的 fileuri 请帮助我。
我的代码:
loadimage = (ImageView) findViewById(R.id.profilpic);
loadimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
/* Intent i = new Intent(Tab1.this, ImageMain.class);
startActivity(i);*/
// selectImageFromGallery();
AlertDialog.Builder builder = new AlertDialog.Builder(Tab1.this);
builder.setMessage("Select Image From")
.setCancelable(true)
.setPositiveButton("camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_REQUEST);
}
})
.setNegativeButton("gallary", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
听说是我的onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
launchUploadActivity(true);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
else if(requestCode==RESULT_LOAD_IMAGE){
if (resultCode==RESULT_OK){
launchUploadActivity(true);
}
}
}
private void launchUploadActivity(boolean isImage){
Intent i = new Intent(Tab1.this,UploadAvtivity.class);
i.putExtra("filePath", fileUri.getPath());
i.putExtra("isImage", isImage);
startActivity(i);
}
这是通过默认相机拍照的代码片段(这里我实现了 Intent 来获取图像)。之后将其存储到SD卡(此处将创建一个新文件并存储新拍摄的图像);如果您不想存储,则从代码中删除保存部分。之后,您可以将文件路径用于上传目的。然后您可以参考它并更改以获取您想要的路径。
在 class 区域放置这些行
final int TAKE_PHOTO_REQ = 100;
String file_path = Environment.getExternalStorageDirectory()
+ "/recent.jpg";//Here recent.jpg is your image name which will going to take
之后通过将这些行放在调用方法中来调用相机。
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PHOTO_REQ);
然后在你的 Activity 中添加这个方法来获取图片并将其保存到 sd 卡,你可以从这里调用你的上传方法,通过知道它的路径上传图片。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO_REQ: {
if (resultCode == TakePicture.RESULT_OK && data != null) {
Bitmap srcBmp = (Bitmap) data.getExtras().get("data");
// ... (process image if necesary)
imageView.setImageBitmap(srcBmp);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
srcBmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(file_path);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// remember close de FileOutput
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("take-img", "Image Saved to sd card...");
// Toast.makeText(getApplicationContext(),
// "Image Saved to sd card...", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
希望这对您和其他人也有帮助..谢谢