Using MediaScannerConnection : MediaScannerConnection « Media « Android






Using MediaScannerConnection

   
package app.test;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Test extends Activity implements MediaScannerConnectionClient {
  private EditText editText = null;
    private String filename = null;
    private MediaScannerConnection conn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText = (EditText)findViewById(R.id.fileName);
    }
    public void startScan(View view) {
        if(conn!=null) {
            conn.disconnect();
        }
        
        filename = editText.getText().toString();

        File fileCheck = new File(filename);
        if(fileCheck.isFile()) {
            conn = new MediaScannerConnection(this, this);
            conn.connect();
        }
        else {
            Toast.makeText(this,
                "That file does not exist",
                Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onMediaScannerConnected() {
        conn.scanFile(filename, null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            if (uri != null) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(uri);
                startActivity(intent);
            }
        } finally {
            conn.disconnect();
            conn = null;
        } 
    }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"  android:layout_height="wrap_content">

  <EditText android:id="@+id/fileName"  android:hint="Enter new filename"
  android:layout_width="fill_parent"  android:layout_height="wrap_content" />
    
  <Button android:id="@+id/scanBtn"  android:text="Add file"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
    android:onClick="startScan" />
    
</LinearLayout>

   
    
    
  








Related examples in the same category