如何将多个图像发送到android中的另一个活动?
Posted
技术标签:
【中文标题】如何将多个图像发送到android中的另一个活动?【英文标题】:How to send multiple images to another activity in android? 【发布时间】:2014-07-07 06:56:22 【问题描述】:在我的应用程序中,我创建了一个自定义相机,它可以捕获图像并将其保存在 SD 卡中。也可以在第二个活动中查看。但它只是替换了我不想要的以前的图像。如何将多张图片发送到第二个活动?
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.surfaceview);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.custom, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
click = (Button) findViewById(R.id.button_capture);
click.setOnClickListener(new OnClickListener()
public void onClick(View arg0)
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
);
ShutterCallback myShutterCallback = new ShutterCallback()
@Override
public void onShutter()
// TODO Auto-generated method stub
;
PictureCallback myPictureCallback_RAW = new PictureCallback()
@Override
public void onPictureTaken(byte[] arg0, Camera arg1)
// TODO Auto-generated method stub
;
PictureCallback myPictureCallback_JPG = new PictureCallback()
@Override
public void onPictureTaken(byte[] arg0, Camera arg1)
// TODO Auto-generated method stub
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
filepath = Environment.getExternalStorageDirectory()+"/testing/"+"test.3gp";
System.out.println("thumbnail path~~~~~~"+filepath);
File file = new File(filepath);
Uri uriTarget = Uri.fromFile(file);
OutputStream imageFileOS;
try
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(CustomCameraActivity.this, "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",filepath.toString());
setResult(1,returnIntent);
finish();
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
// camera.startPreview();
;
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
if(previewing)
camera.stopPreview();
previewing = false;
if (camera != null)
try
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
catch (IOException e)
e.printStackTrace();
@Override
public void surfaceCreated(SurfaceHolder holder)
camera = Camera.open();
@Override
public void surfaceDestroyed(SurfaceHolder holder)
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
【问题讨论】:
将此路径存储在 List 中并将此 List 传递给另一个 Activity 并尝试从 List 加载所有图像。 您提到您将图像保存在 SD 卡中。所以你必须有他们的路径。您可以从 sd-card 中选择这些图像。 我只得到一张图片 输入您的代码以从自定义相机获取图像。 我已经更新了我的自定义相机代码 【参考方案1】:只需将您的图像转换为位图,然后通过yourIntent.putExra("key",bitmap);
将Bitmap
传递到另一个活动中
或者使用这个:
private File imageFile;
private Uri imageUri;
private static final int CAMERA_REQUEST_CODE = 100;
private String imagePath
private Uri getTempUri()
// Create an image file name
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment.getExternalStorageDirectory()
+ "/foldername/", "Camera_" + dt + ".jpg");
Applog.Log(
TAG,
"New Camera Image Path:- "
+ Environment.getExternalStorageDirectory()
+ "/foldername/" + "Camera_" + dt + ".jpg");
File file = new File(Environment.getExternalStorageDirectory()
+ "/foldername");
if (!file.exists())
file.mkdir();
if (!imageFile.exists())
try
imageFile.createNewFile();
catch (IOException e)
e.printStackTrace();
imagePath = Environment.getExternalStorageDirectory() + "/foldername/"
+ "Camera_" + dt + ".jpg";
imageUri = Uri.fromFile(imageFile);
return imageUri;
现在当你开始为相机活动时。
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT,
getTempUri());
startActivityForResult(cameraIntent,
CAMERA_REQUEST_CODE);
现在在 ActivityResult 中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (CAMERA_REQUEST_CODE == requestCode && resultCode == RESULT_OK)
String yourUri=ImagePath;
//now store this Uri it will contain capture image url.
【讨论】:
而不是传递 Bitmap 尝试传递 imageuri。 但他/她没有指定要传递 imageUri。 我猜,如果你传递位图,内存会增加。而是通过 ImageUri。以上是关于如何将多个图像发送到android中的另一个活动?的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据从一个活动传递到android中的另一个活动片段? [复制]
如何使用数据库将大量数据从一个活动传递到android中的另一个活动