我想从设备gallary中选择图像并将其上传到 amazon s3 服务器,但我在运行时有一个例外,我不知道为什么会得到它。
堆栈跟踪:
05-22 13:17:46.939 23624-23624/com.example.mmido.trivelgob2b E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mmido.trivelgob2b, PID: 23624
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/19106 }} to activity {com.example.mmido.trivelgob2b/com.example.mmido.trivelgob2b.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3661)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3704)
at android.app.ActivityThread.access$1300(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:355)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:128)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:356)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4375)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1704)
at com.example.mmido.trivelgob2b.MainActivity.onActivityResult(MainActivity.java:78)
at android.app.Activity.dispatchActivityResult(Activity.java:5456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3657)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3704)
at android.app.ActivityThread.access$1300(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5398)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
从画廊中选择图片的按钮事件:
public void SelectImage(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
onActivityResult:(执行此方法中写入的最后一行时出现异常)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
File f = new File(picturePath);
String imageName = f.getName();
AmazonS3Client s3Client = new AmazonS3Client( new BasicAWSCredentials( MY_ACCESS_KEY, MY_SECRET_KEY ) );
s3Client.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));
PutObjectRequest por = new PutObjectRequest( "BUCKET_NAME", FOLDER_NAME+"/"+imageName,f );
s3Client.putObject(por);
Mainfest permisions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Gradle依赖项:
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "cz.msebera.android:httpclient:4.4.1.1"
compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.2.2'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.amazonaws:aws-android-sdk-core:2.+'
compile 'com.amazonaws:aws-android-sdk-cognito:2.+'
compile 'com.amazonaws:aws-android-sdk-s3:2.+'
compile 'com.amazonaws:aws-android-sdk-ddb:2.+'
答案 0 :(得分:3)
我发现上传到亚马逊应该是不在主线程中的另一个线程。所以我添加了扩展AsyncTask的UploadImage类,如下所示:
public class UploadImage extends AsyncTask<String,String,String> {
private final String accessKey="my_access_Key";
private final String secretKey="my_secret_key";
private final String bucketName="my_bucket_name";
private final String folderName="Android_Uploads"; /*"Android_Uploads";*/
@Override
protected String doInBackground(String... params) {
String fileName = params[0];
String filePath = params[1];
AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(accessKey,secretKey));
s3Client.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));
PutObjectRequest por = new PutObjectRequest(bucketName, folderName +"/"+ fileName, new File(filePath));
s3Client.putObject(por);
return "";
}
protected void onPostExecute(String feed) {
return ;
}
}
我在主要活动的onActivityResult中使用它:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
File f = new File(picturePath);
String imageName = f.getName();
new UploadImage().execute(imageName, picturePath);