Check a File Existence and Read/Write it

While developing a app, I used internal storage to save a file.

  1. Firstly, I needed to check a file existence. For me, this worked. (https://stackoverflow.com/questions/8867334/check-if-a-file-exists-before-calling-openfileinput)
    I used like this but it didn’t work. In my thought, it is because I called it in another thread and it made a problem.
  2. There are some functions to do read/write. This page helped me to decide which to use.
    (https://stackoverflow.com/questions/11371154/outputstreamwriter-vs-filewriter)
  3. Finally, this page solved my problem. It was also related to the thread as I thought.
    (https://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android)

 

Advertisements

NavigationView Error While Running an App on a Device

I met NavigationView Error while running my app on a device (API 19).

Error inflating class android.support.design.widget.NavigationView

It worked well on emulator. Error log showed details like this.

android.view.InflateException: Binary XML file line #11: Error inflating class android.support.design.widget.NavigationView

android.content.res.Resources$NotFoundException: File res/drawable/bg_nav.xml from drawable resource ID #0x7f020046

Finally, I found a solution. (But, I don’t know the exact reason…)\
My resources were only in drawable-v21 and I copied them to drawable.
(https://stackoverflow.com/questions/30709419/error-inflating-class-android-support-design-widget-navigationview)

How to Connect Android Application with Database in Server, Amazon EC2

  1. Set Up the AWS Mobile SDK for Android (http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/setup.html)
    1. I used Android Studio and added the aws-android-sdk-core dependency to my app/build.gradle file, along with the dependencies for the individual services that my project will use, as shown below.
      dependencies {
          compile 'com.amazonaws:aws-android-sdk-core:2.2.+'
          // In the document, they used s3 but I used ec2
          compile 'com.amazonaws:aws-android-sdk-ec2:2.2.+'
          compile 'com.amazonaws:aws-android-sdk-ddb:2.2.+'
          // If you don't add ddb-mapper, compiler can't find         com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper which will be imported later.
          compile 'com.amazonaws:aws-android-sdk-ddb-mapper:2.2.+'
      }
      
  2. Where Should I place AWS Credentials? I referred here.
    (https://www.numetriclabz.com/integrate-amazon-s3-to-android-tutorial/)

    public class MainActivity extends AppCompatActivity {
        AmazonS3 s3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // callback method to call credentialsProvider method.
            credentialsProvider();
        }
    
        public void credentialsProvider(){
            // Initialize the Amazon Cognito credentials provider
            CognitoCachingCredentialsProvider credentialsProvider = new     CognitoCachingCredentialsProvider(
                getApplicationContext(),
                us-east-1:dbacd6aa-9393-475e-b687-xxxxxxxx;, // Identity Pool ID
                Regions.US_EAST_1 // Region
            );
            setAmazonS3Client(credentialsProvider);
        }
    /**
    *  Create a AmazonS3Client constructor and pass the credentialsProvider.
    * @param credentialsProvider
    */
        public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider) {
            // Create an S3 client
            s3 = new AmazonS3Client(credentialsProvider);
            // Set the region of your S3 bucket
            s3.setRegion(Region.getRegion(Regions.US_EAST_1));
        }
    }
    
    
  3. Store and Retrieve App Data in Amazon Dynamo DB
    (http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/dynamodb_om.html)

    1. Create a DynamoDB Table
      I couldn’t find a way to add attributes when I created a table.
      Then I should add them whenever I add new items.
    2. Set Permissions
      1. Log in to the IAM Console.
      2. Select Roles and select the “Unauth” role that Cognito created for you.
      3. Click Edit Policy in Inline Policies.
      4.  Enter a name for your policy and paste in the policy document shown the document, replacing the Resource values with the ARNs for your table and index. (You can retrieve the table ARN from the Details tab of database; then append /index/* to obtain the value for the index ARN.
      5. Click Apply Policy.
    3. To establish mappings, DynamoDB defines annotations.
    4. Specify the region when you create your AmazonDynamoDBClient.
      (https://stackoverflow.com/questions/34223123/cognito-dynamodb-not-authorized-to-perform-dynamodbupdateitem-on-resource)
    5.  Interacting with stored objects is synchronous and must be taken off of the main thread.