Skip to main content

Featured

Android studio “SDK tools directory is missing”

Following 2 possible solutions will resolve this problem :  Solution1 : To fix the problem, it was required that I list the path to my corporate PAC file by using  Configure -> "Appearance and Behavior" -> System Settings -> HTTP Proxy . I selected "Automatic proxy configuration url:" Delete your  ~/.Android*  folders (losing all of your settings :/). Run Android Studio. It will show you a welcome wizard where it tries to download the SDK again (and fails due to my rubbish internet). Click the X on the wizard window. That will enable you to get to the normal welcome dialog. Go to Settings->Project Defaults->Project Structure and change the Android SDK location to the correct one. Solution 2 : To fix the problem, it was required that I list the path to my corporate PAC file by using  Configure -> "Appearance and Behavior" -> System Settings -> HTTP Proxy . I selected "Automatic proxy configuration url:...

Code to display user created project when user clicks attachment file in any email(like Gmail) inbox and download that attachment to SDCard/ApplicationDir after clicking it..Android

OptionsInMailAttachmntsOpenActivity.java:-

package org.dharani.mailAttacmntOpen;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.widget.Button;

public class OptionsInMailAttachmntsOpenActivity extends Activity {
   
    WebView mWebView;
    Button button;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      // String myScript="<html><iframe src='http://docs.google.com/gview? url='http://mymobilece.com/api/api_getexammaterials.php?id=28'&embedded='true' style='width:600px; height:500px;' frameborder='0'></iframe></html>";
       /* mWebView=(WebView)findViewById(R.id.webView1);    
        mWebView.setKeepScreenOn(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.setInitialScale(100);
        // wbView.getSettings().setUseWideViewPort(true);
        mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.setWebViewClient(new WebViewClient());

        // Set JS alert() hook
        mWebView.setWebChromeClient(new WebChromeClient() {
            public boolean onJsAlert(WebView view, String url, String message, JsResult result)
            {
                return false;
            }
            });         
        //mWebView.loadData(myScript,"text/html", "UTF-8");
        mWebView.loadUrl("file:///android_asset/index.html");   // now it will not fail here
        // Add JS libraries
        mWebView.addJavascriptInterface(new message(), "Show");
       
        button=(Button)findViewById(R.id.button);  */
       
        Intent i = getIntent();
        if(i == null) return;
        Uri u = i.getData();
        if(u == null) return;
        String scheme = u.getScheme();
       
        //this block is not necessary...
        mWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
              Intent i = new Intent(Intent.ACTION_VIEW);
              i.setType(mimetype);
              i.setData(Uri.parse(url));
              startActivity(i);
            }
        });
       
      
       
        if(ContentResolver.SCHEME_CONTENT.equals(scheme))
        {
           
            Uri uri = getIntent().getData();
            String name = getContentName(getContentResolver(), uri);
            System.out.println("******email attachment name***********"+name);
            String emailFileName = getEmailFileName(getContentResolver(), uri);
            System.out.println("******email file name***********"+emailFileName);
            InputStream is = null;
            try {
                is = this.getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.out.println("******input stream***********"+is.toString());

            System.out.println("******my application-1***********"+scheme.toString());
            System.out.println("******u***********"+u.getPath());
            System.out.println("******my application-1***********"+ContentResolver.SCHEME_CONTENT);
            String filePath = u.getEncodedPath();
            System.out.println("******filePath***********"+filePath);
           
          
            ///code to download and save Url attachment to application local directory
            try {
                InputStream attachment = getContentResolver()
                        .openInputStream(getIntent().getData());
                File savedFile = new File(Environment
                        .getExternalStorageDirectory().getAbsolutePath(),
                       name);
                FileOutputStream f = new FileOutputStream(savedFile);
                byte[] buffer = new byte[1024];
                while ((attachment.read(buffer)) > 0) {
                    f.write(buffer);
                }
                f.close();
            } catch (Exception e) {
            }
            InputStream attachment = null;
        
          
            try {
                attachment = getContentResolver()
                        .openInputStream(getIntent().getData());
                File savedFile = new File(Environment
                        .getExternalStorageDirectory().getAbsolutePath(),
                        name);
                FileOutputStream f = new FileOutputStream(savedFile);
                byte[] buffer = new byte[1024];
                while ((attachment.read(buffer)) > 0) {
                    f.write(buffer);
                }
                f.close();
            } catch (Exception e) {
            }
           
          //pdf or .doc or .docx or .txt files...
               if(name.contains(".pdf") || name.contains(".doc") || /*name.contains(".docx") ||*/ name.contains(".txt") )
               {   
                 
               }
           
           
           /*try
            {
                ContentResolver cr = getContentResolver();
                AssetFileDescriptor afd = cr.openAssetFileDescriptor(u, "r");
                long length = afd.getLength();
                byte[] filedata = new byte[(int) length];
                InputStream is = cr.openInputStream(u);
                if(is == null) return;
                try
                {
                    is.read(filedata, 0, (int) length);
                    Util.loadOTDRFileFromByteArray(filedata);
                }
                catch(IOException e)
                {
                    return;
                }  
            }
            catch(FileNotFoundException e)
            {
                return;
            } 
            }
            catch(FileNotFoundException e)
            {
                return;
            }*/
        }
        else
        {
            //System.out.println("******my application-2***********");
           // String filePath = u.getEncodedPath();
            //if(filePath == null) return;
            //Util.loadOTDRFileFromPath(filePath);
        }
    }
   
    public void buttonClick(View v){
       
        //mWebView.setWebViewClient(new HelloWebViewClient());
        //mWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
        //mWebView.loadUrl(URL);
        //Log.v("TheURL", URL);
       
    }
   
   
    public static String getContentName(ContentResolver resolver, Uri uri){
        Cursor cursor = resolver.query(uri, null, null, null, null);
        cursor.moveToFirst();
        int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
        if (nameIndex >= 0) {
            return cursor.getString(nameIndex);
        } else {
            return null;
        }
    }
   
   
    public static String getEmailFileName(ContentResolver resolver, Uri uri){
        Cursor cursor = resolver.query(uri, new String[]{"_display_name"}, null, null, null);
        cursor.moveToFirst();
        int nameIndex = cursor.getColumnIndex("_display_name");
        if (nameIndex >= 0) {
            return cursor.getString(nameIndex);
        } else {
            return null;
        }
        }
   
   
    /*public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try {
          URL urlObj = new URL(url);
          if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
            //Allow the WebView in your application to do its thing
            return false;
          } else {
            //Pass it to the system, doesn't match your domain
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            //Tell the WebView you took care of it.
            return true;
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        return true;
    }*/
  
    class message {
        public String msg() {
            return "Hello World!!";
        }
    }
}


AndroidManifest.xml:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.dharani.mailAttacmntOpen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="com.android.email.permission.ACCESS_PROVIDER" />
    <uses-permission android:name="com.android.email.permission.READ_ATTACHMENT" />
    <uses-permission android:name="com.google.android.gm.permission.READ_ATTACHMENT_PREVIEW" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET" />
   
  
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".OptionsInMailAttachmntsOpenActivity"
            android:label="@string/app_name" >
           
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           <!--  <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:host="*" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\\.txt" />
            </intent-filter> -->
           
                       
             <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:mimeType="*/*" />               
                <data android:host="*" />
                <data android:port="*" />
                <data android:pathPattern=".*..*..*..*..*..*.trc" />
                <data android:pathPattern=".*..*..*..*..*.trc" />
                <data android:pathPattern=".*..*..*..*.trc" />
                <data android:pathPattern=".*..*..*.trc" />
                <data android:pathPattern=".*..*.trc" />
                <data android:pathPattern=".*.trc" />
            </intent-filter>

            <!-- <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
                <data android:host="*" />
                <data android:mimeType="*/*" />
            <data android:pathPattern=".*\\.xyz" />
            </intent-filter> -->
           
        </activity>
    </application>

</manifest>

Comments