gpt4 book ai didi

Java-Android-FTP上传主线程异常

转载 作者:行者123 更新时间:2023-12-01 10:05:37 25 4
gpt4 key购买 nike

我正在开发一个非常简单的应用程序,您按下一个按钮,启动相机,您拍摄一张照片,这张照片存储在 SD 卡中,然后上传到我的 FTP。我正在使用 simpleFTP 库连接到我的 FTP,但我不知道为什么在上传文件时出现以下错误。

这是主类代码的相关部分:

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
Log.v("photoFile", photoFile.toString());
} catch (IOException ex) {
// Error occurred while creating the File

}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}

}

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//The image is already stored in the phone, let's open it up from there
File imgFile = photoFile;
if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

mImageView.setImageBitmap(myBitmap);

}

new UploadFTP<File>().doInBackground(imgFile);
}
}

这是处理 AsycnTask 的类:

class UploadFTP<File> extends AsyncTask<File, Void, Void> {


@Override
protected Void doInBackground(File... params) {
File file = params[0];
try
{
SimpleFTP ftp = new SimpleFTP();

// Connect to an FTP server on port 21.
ftp.connect("ftp.myurl", 21, "myuser", "mypass");

// Set binary mode.
ftp.bin();

// Change to a new working directory on the FTP server.
ftp.cwd("www/xxx");

// Upload files.
ftp.stor(new java.io.File(String.valueOf(file)));

ftp.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}

这里的错误:

FATAL EXCEPTION: main
Process: com.ignistudios.photosharing, PID: 22214
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ignistudios.photosharing/com.ignistudios.photosharing.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3607)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650)
at android.app.ActivityThread.access$1400(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
at java.net.Socket.startupSocket(Socket.java:590)
at java.net.Socket.tryAllAddresses(Socket.java:128)
at java.net.Socket.<init>(Socket.java:178)
at java.net.Socket.<init>(Socket.java:150)
at org.jibble.simpleftp.SimpleFTP.connect(SimpleFTP.java:68)
at com.ignistudios.photosharing.UploadFTP.doInBackground(UploadFTP.java:26)
at com.ignistudios.photosharing.MainActivity.onActivityResult(MainActivity.java:139)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3603)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650) 
at android.app.ActivityThread.access$1400(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5294) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

最佳答案

您使用AsyncTask的方式是错误的。在其上调用 execute,它会自动在允许网络操作的不同线程上调用 doInBackground()

UploadFTP uploadFTP = new UploadFTP();
uploadFTP.execute();

关于Java-Android-FTP上传主线程异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36497320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com