Android中的图像捕获和上传到FTP
我面临着一个我自己无法解决的问题 我必须在android应用程序中捕获图像,然后将图像上传到FTP服务器。当然,在将其发送到FTP之前,我必须调整其大小,因为2MB绝对是不可接受的大小:) 我成功地拍摄了这张照片,得到了它的路径,并上传了它的全尺寸 这是我上传到服务器的方式Android中的图像捕获和上传到FTP,android,image,ftp,Android,Image,Ftp,我面临着一个我自己无法解决的问题 我必须在android应用程序中捕获图像,然后将图像上传到FTP服务器。当然,在将其发送到FTP之前,我必须调整其大小,因为2MB绝对是不可接受的大小:) 我成功地拍摄了这张照片,得到了它的路径,并上传了它的全尺寸 这是我上传到服务器的方式 File file = new File(pathOfTheImage); String testName =System.currentTimeMillis()+file.getName(); fis = new File
File file = new File(pathOfTheImage);
String testName =System.currentTimeMillis()+file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);
在这一点上,是否可以调整或压缩图像以减小其大小,然后将其上载到服务器
任何帮助都将不胜感激
另外,对不起,我的英语很差
编辑:
多亏了阿拉莫里。
再来一次,老兄,谢谢你 在我的应用程序中,我在上传图像之前使用了这个:
1-调整位图的大小、缩放和解码
private Bitmap decodeFile(File f) {
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
final int REQUIRED_SIZE=450;
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
return bit1;
} catch (FileNotFoundException e) {}
return null;
}
2-现在让我们保存调整大小的位图:private void ImageResizer(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Pic");
if(!myDir.exists()) myDir.mkdirs();
String fname = "resized.im";
File file = new File (myDir, fname);
if (file.exists()){
file.delete();
SaveResized(file, bitmap);
} else {
SaveResized(file, bitmap);
}
}
private void SaveResized(File file, Bitmap bitmap) {
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
保存已调整大小的缩放图像后。使用您的代码上传它:String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Pic/resized.im");
String testName =System.currentTimeMillis()+file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);
你的英语很好。退房希望这能有所帮助。@Yatin Saraiya谢谢你(快速)的回答!我的问题是,在这一点上,我只有路径的(全尺寸)图像。最好怎么做?很抱歉有这么多问题,但这确实是我一直在努力解决的问题,不幸的是,我没有成功。您需要下载图像以重新调整大小或压缩它。你可以在上传到服务器后删除它。老兄,我不知道该如何感谢你!!!!!!!!!!你救了我,我不知道该如何感谢你!谢谢你,一百万次!!!没问题,但别忘了更改保存的图像扩展名,我使用了
.im
,您可以使用JPG、PNG……)@乔文,不客气,另一位推荐。。不使用FTP是不安全的。。使用多次上传。是的,当然,但这正是我花了这么多时间试图做的事情,我更关注图像大小调整,而不是更重要的事情:)