0

Currently working on handling of multiple type of files and allowing them to open with there default app in android phone. As far till now i am able to open all others file like : pdf,image,audio with there default app installed in the phone. but when t comes to office files like xlxs, doc. Already installed OFFICE SUITE but i was unable to open them.

Code to open with its default app:

@Override
public void onSelectedItem(String image_urls_, int position) {
String mimeType = getMimeType(image_urls_);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(image_urls_), mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("memtype", mimeType);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(Attachments.this, "No handler for this type of file.", Toast.LENGTH_LONG).show();
Log.e("bug_file", e.toString());
 }
} 

code to find out type of file

public static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = 
    MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}
Viney Dhiman
  • 183
  • 1
  • 3
  • 15

1 Answers1

0

I used WebView and ImageView to show the documents in inside app itself.

xml :

<RelativeLayout
    android:id="@+id/rl_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/attachment"/>

    <ProgressBar
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:id="@+id/progress_bar"
        android:layout_centerInParent="true" />

</RelativeLayout>

Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attachement_detail_activity);

        webView = (WebView) findViewById(R.id.web_view);
        View rlImage = findViewById(R.id.rl_image);
        ImageView imageView = (ImageView) findViewById(R.id.iv_image_view);

        fileUrl = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
        courseName = getIntent().getStringExtra(Constants.BUNDLE_COURSE_TITLE);
        courseId = getIntent().getIntExtra(Constants.BUNDLE_COURSE_ID, 0);
        if(fileUrl.contains(".jpg") || fileUrl.contains(".jpeg") || fileUrl.contains(".png")) {
            AQuery aQuery = new AQuery(this);
            aQuery.id(imageView).progress(R.id.progress_bar).image(fileUrl, false, true, Helper.getDisplayWidth(this), 0);
            rlImage.setVisibility(View.VISIBLE);
            webView.setVisibility(View.GONE);
        } else {
            rlImage.setVisibility(View.GONE);
            showDoc(fileUrl);
            webView.setVisibility(View.VISIBLE);
        }
    }

    private void showDoc(final String fileUrl){
        if (Helper.isConnected(this)) {
            final Dialog progressDialog = new Dialog(this);
            if (progressDialog.getWindow() != null) {
                progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            }
            progressDialog.setContentView(R.layout.custom_progress_layout);
            progressDialog.setCancelable(false);
            progressDialog.setTitle(this.getString(R.string.app_name));

            if (progressDialog.getWindow() != null) {
                progressDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                progressDialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL);
            }
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    progressDialog.show();
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    progressDialog.dismiss();
                }
            });
//            String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
            webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + fileUrl);
        }
    }

Here I used Android Query Library to show image from URL library

If you want to use download after view this file, you can provide download option to user to download. Use download manager to download the file from url how to use download manager

Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74