Android - 从Amazon s3存储桶获取图像对象时,活动崩溃

时间:2014-03-21 01:39:18

标签: android amazon-s3 amazon bucket

我确信凭证是正确的,因为我可以将图像上传到S3存储桶中。但是当我试图从同一个桶中获取图像对象时。我的活动崩溃了。我不知道问题出在哪里。当我使用

时,我尝试了很多方法来获取对象
S3Object object = s3Client.getObject( Constant.getPictureBucket(), "1264" ); 

它崩溃了 P.S:当我切换这些获取对象部分的注释时。活动有效。我是新来的。谢谢大家。感谢所有的答案!!

    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  

    import org.apache.commons.io.IOUtils;  

    import android.os.Bundle;  

    import com.amazonaws.auth.BasicAWSCredentials;  
    import com.amazonaws.regions.Region;  
    import com.amazonaws.regions.Regions;    
    import com.amazonaws.services.s3.AmazonS3Client;  
    import com.amazonaws.services.s3.model.S3Object;  
    import com.amazonaws.services.s3.model.S3ObjectInputStream;  

    public class BoxActivity extends MainActivity 
    {       
            private AmazonS3Client s3Client = new AmazonS3Client( 
            new BasicAWSCredentials(Constant.ACCESS_KEY_ID, Constant.SECRET_KEY));  
            @Override  
            public void onCreate(Bundle savedInstanceState)   
            {
                    super.onCreate(savedInstanceState);  
                    setContentView(R.layout.box);  
                    s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));      
                    S3Object object = s3Client.getObject( Constant.getPictureBucket(), "1264" );  
                    S3ObjectInputStream objectContent = object.getObjectContent();  
                    try {  
                            IOUtils.copy(objectContent,newFileOutputStream("D://upload//test.jpg"));  
            }   
                    catch (FileNotFoundException e) {               
                e.printStackTrace();  
            } catch (IOException e) {          
                e.printStackTrace();
            }
        }

    }

布局XML

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TextView
        android:name="@string/box"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TextView>
    </LinearLayout> 

LogCat :(只是红色错误!)

    E/AndroidRuntime(1650): java.lang.RuntimeException: Unable to start activity ComponentInfo{BoxActivity}: android.os.NetworkOnMainThreadException  
    E/AndroidRuntime(1650): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)  
    E/AndroidRuntime(1650): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    E/AndroidRuntime(1650):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    E/AndroidRuntime(1650):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    E/AndroidRuntime(1650):     at android.os.Handler.dispatchMessage(Handler.java:102)
    E/AndroidRuntime(1650):     at android.os.Looper.loop(Looper.java:136)
    E/AndroidRuntime(1650):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    E/AndroidRuntime(1650):     at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(1650):     at java.lang.reflect.Method.invoke(Method.java:515)
    E/AndroidRuntime(1650):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    E/AndroidRuntime(1650):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    E/AndroidRuntime(1650):     at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime(1650): Caused by: android.os.NetworkOnMainThreadException
    E/AndroidRuntime(1650):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
    E/AndroidRuntime(1650):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    E/AndroidRuntime(1650):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    E/AndroidRuntime(1650):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    E/AndroidRuntime(1650):     at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    E/AndroidRuntime(1650):     at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:377)
    E/AndroidRuntime(1650):     at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
    E/AndroidRuntime(1650):     at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3215)
    E/AndroidRuntime(1650):     at com.amazonaws.services.s3.AmazonS3Client.getObject(AmazonS3Client.java:1038)
    E/AndroidRuntime(1650):     at com.amazonaws.services.s3.AmazonS3Client.getObject(AmazonS3Client.java:920)
    E/AndroidRuntime(1650):     at BoxActivity.onCreate(BoxActivity.java:27)
    E/AndroidRuntime(1650):     at android.app.Activity.performCreate(Activity.java:5231)
    E/AndroidRuntime(1650):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    E/AndroidRuntime(1650):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

0 个答案:

没有答案