Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:39:16+00:00 2026-06-10T14:39:16+00:00

In my Android App when I am try to download image file from Amazon

  • 0

In my Android App when I am try to download image file from Amazon S3 it gives me exception

Here It is exception:

08-30 11:20:34.157: W/System.err(6519): android.os.NetworkOnMainThreadException
08-30 11:20:34.157: W/System.err(6519): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-30 11:20:34.157: W/System.err(6519):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-30 11:20:34.157: W/System.err(6519):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-30 11:20:34.157: W/System.err(6519):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-30 11:20:34.157: W/System.err(6519):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-30 11:20:34.168: W/System.err(6519):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-30 11:20:34.168: W/System.err(6519):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-30 11:20:34.168: W/System.err(6519):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-30 11:20:34.177: W/System.err(6519):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-30 11:20:34.177: W/System.err(6519):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-30 11:20:34.177: W/System.err(6519):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-30 11:20:34.177: W/System.err(6519):     at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:285)
08-30 11:20:34.177: W/System.err(6519):     at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:167)
08-30 11:20:34.187: W/System.err(6519):     at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:2781)
08-30 11:20:34.187: W/System.err(6519):     at com.amazonaws.services.s3.AmazonS3Client.getObject(AmazonS3Client.java:872)
08-30 11:20:34.187: W/System.err(6519):     at com.example.amazons3.AmazonS3DemoActivity.Download(AmazonS3DemoActivity.java:74)
08-30 11:20:34.187: W/System.err(6519):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 11:20:34.187: W/System.err(6519):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 11:20:34.187: W/System.err(6519):     at android.view.View$1.onClick(View.java:3039)
08-30 11:20:34.197: W/System.err(6519):     at android.view.View.performClick(View.java:3511)
08-30 11:20:34.197: W/System.err(6519):     at android.view.View$PerformClick.run(View.java:14105)
08-30 11:20:34.197: W/System.err(6519):     at android.os.Handler.handleCallback(Handler.java:605)
08-30 11:20:34.197: W/System.err(6519):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-30 11:20:34.207: W/System.err(6519):     at android.os.Looper.loop(Looper.java:137)
08-30 11:20:34.207: W/System.err(6519):     at android.app.ActivityThread.main(ActivityThread.java:4424)
08-30 11:20:34.207: W/System.err(6519):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 11:20:34.207: W/System.err(6519):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 11:20:34.217: W/System.err(6519):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-30 11:20:34.227: W/System.err(6519):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-30 11:20:34.227: W/System.err(6519):     at dalvik.system.NativeStart.main(Native Method)

Here it my java Code:

public class AmazonS3Activity extends Activity 
{
private String Tag = "Downlaod";

Button btnDownload;

Context myContext;

String product_code = "pro_code";

String bucketName = "bucketnamw";

String appDirPath = Environment.getExternalStorageDirectory().getName() +"/foldername/";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myContext = this;

    btnDownload = (Button) findViewById(R.id.btnDownload);      
}

public void Download(View button)
{
    try 
    {
        String imgName = product_code+"1.jpg";
        String md5Name = getMD5HasCode(product_code);

        String productCode = md5Name+".app/";

        String filePath = getFullFilePath(imgName);

        String accessKey = "accessKey";
        String secretKey = "secretKey";
        AWSCredentials credential = new BasicAWSCredentials(accessKey, secretKey);

        Log.e(Tag, "FileName : " + filePath, null);

        String strObjectKey = productCode + imgName.replace(product_code,md5Name);

        Log.e(Tag, "Object Key : " + strObjectKey, null);

        GetObjectRequest objRequest = new GetObjectRequest(bucketName,strObjectKey);

        AmazonS3Client myS3Client = new AmazonS3Client(credential);

        S3Object s3Object = myS3Client.getObject(objRequest);

        long fileSize = s3Object.getObjectMetadata().getContentLength();
        final InputStream input = s3Object.getObjectContent();
        final FileOutputStream fos = new FileOutputStream(filePath);

        long total = 0;
        int len=0;

        byte[] buf = new byte[1024];

        while((len = input.read(buf)) > 0)
        {
            fos.write(buf,0,len);

            total = total + len;
        }

        if(len == -1 && total==fileSize)
        {
            Thread.currentThread().interrupt();

            input.close();
            fos.close();

        }

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        Log.e(Tag, "Error Key : " + e.getMessage(), null);
    }

}

public String getMD5HasCode(String strMessage)
{
    try
    {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] messageDigestBytes = messageDigest.digest(strMessage.getBytes());
        BigInteger hasNumber = new BigInteger(1,messageDigestBytes);
        String md5 = hasNumber.toString(16);

        while(md5.length()<32)
        {
            md5 = "0" + md5;
        }

        return md5;
    }
    catch (Exception ex) 
    {
        Toast.makeText(myContext, "ERROR in HasCode : "+ex.toString(), Toast.LENGTH_SHORT).show();  
        return null;
    }
}

public String getFullFilePath(String filename)
{
    File dir = new File(appDirPath);
    if(!dir.exists())
    {
        dir.mkdir();
    }
    return appDirPath+filename;
}
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:39:18+00:00Added an answer on June 10, 2026 at 2:39 pm

    As of the documentation found here.

    NetworkOnMainThreadException is thrown when an application attempts to perform a
    networking operation on its main thread.

    This is only thrown for applications targeting the Honeycomb SDK or
    higher. Applications targeting earlier SDK versions are allowed to do
    networking on their main event loop threads, but it’s heavily
    discouraged.

    The solution can be using a AsyncTask for Network related operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to download a file from FTP via my app. But neither
in my Android app I need to download a ~40 MB file, and I'm
I'm making an Android app where the user can download files from a FTP-server.
I want to download image from internet :: I refer this and try this
I try to download file from the internet with this method: try { URL
I have android device with root and i try to implement some small app.
The android app reads from database but doesnt write anything to DB, the app
my android app crash before load main.xml. This is the exception thrown java.lang.RuntimeException: Unable
In my application, I'm trying to download a file from my website, but I'm
In my Android app, I try to connect to a port on a local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.