package id.ac.istn.appkripto;
//https://www.dev2qa.com/android-read-write-external-storage-file-example/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class appSD extends Activity {
private TextView textView;
String TAG="isiq";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.txt1);
bacaFile("/storage/sdcard0/FILEPROJEK/en.uji5.txt");
bacaisiSD();
// bacaFile("/storage/sdcard0/FILEPROJEK/en.uji1.pdf");
// bacaFile("/storage/sdcard0/FILEPROJEK/dec.en.uji1.pdf");
//buatfile();
// bacaFile();
// String pathsd=getPathSD()+File.separator+"myData";
// Log.v("pathsd",pathsd);
// createDirIfNotExists(pathsd);
//
// baca(pathsd+File.separator+"tes.txt");
// bacaFile();
// File folder = new File(Environment.getExternalStorageDirectory().toString()+"\\myDataAPPA");
// folder.mkdirs();
//
// String extStorageDirectory = folder.toString();
// Log.v("extStorageDirectory",extStorageDirectory);
// // bacaisiSD();
// //copyFileOrDirectory("/storage/sdcard0/tes1.txt","/storage/sdcard0/FILEPROJEK/");
// try {
// copy("/storage/sdcard0/tes1.txt","/storage/sdcard0/FILEPROJEK/tes1.txt");
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
void buatfile(){
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//handle case of no SDCARD present
} else {
String dir = Environment.getExternalStorageDirectory()+File.separator+"ADI";
//create folder
File folder = new File(dir); //folder name
folder.mkdirs();
Log.v("BUAT","Berhasil="+dir);
Toast.makeText(this, dir, Toast.LENGTH_LONG).show();
//create file
File file = new File(dir, "filename.txt");
File files = new File(dir, "out.txt");
FileOutputStream os = null;
try {
os = new FileOutputStream(files);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String datas ="Selamat Pagi Bro kamu OK?";// "This is the content of my file";
try {
os.write(datas.getBytes());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//++++++++++++++++++++++++++++++
// bacaisiSD();
// bacaFile("/storage/sdcard0/uji1.txt");
// bacaFile("/storage/sdcard0/FILEPROJEK/en.uji1.txt");
// bacaFile("/storage/sdcard0/FILEPROJEK/dec.uji1.txt");
}
}
public void copy(String ssrc, String sdst) throws IOException {
File src=new File(ssrc);
File dst=new File(sdst);
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
void bacaisiSD(){
final String state = Environment.getExternalStorageState();
File file = Environment.getExternalStorageDirectory();
String abs=file.getAbsolutePath();
Log.v("isiabs",abs);
if ( Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) ) { // we can read the External Storage...
getAllFilesOfDir(Environment.getExternalStorageDirectory());
}
}
private void getAllFilesOfDir(File directory) {//Streaming.jpg
// //String al="/storage/sdcard0/WhatsApp/Media/WhatsApp Documents/";//directory.getAbsolutePath()+"/FILEPROJEK/";
//
// File mfile = Environment.getExternalStorageDirectory();
//String abs=mfile.getAbsolutePath();
// Log.v("isiabs",abs);
//String al=abs+"/FILEPROJEK/";
// Log.v("isiabs2",al);
// directory=new File(al);
String abs=directory.getAbsolutePath();
Log.v("isiabs",abs);
String al=abs+"/FILEPROJEK/";
directory=new File(al);
Log.d(TAG, "Directory: " + directory.getAbsolutePath() + "\n");
final File[] files = directory.listFiles();
if ( files != null ) {
for ( File file : files ) {
if ( file != null ) {
if ( file.isDirectory() ) { // it is a folder...
getAllFilesOfDir(file);
}
else { // it is a file...
Log.d(TAG, "File: " + file.getAbsolutePath() + "\n");
}
}
}
}
}
void bacaFile(String baca){
String state = Environment.getExternalStorageState();
if (!(state.equals(Environment.MEDIA_MOUNTED))) {
Toast.makeText(this, "There is no any sd card", Toast.LENGTH_LONG).show();
} else {
BufferedReader reader = null;
try {
File file = Environment.getExternalStorageDirectory();
String abs=file.getAbsolutePath();
Log.v("isiabs",abs);
// Toast.makeText(this, "Sd card available "+abs, Toast.LENGTH_LONG).show();
// File textFile = new File(file.getAbsolutePath()+File.separator+ "tes1.txt");
//String abs2=textFile.getAbsolutePath();
// Log.v("isiabs2",abs2);
// Toast.makeText(this, "Sd card available "+abs2, Toast.LENGTH_LONG).show();
//
reader = new BufferedReader(new FileReader(baca));//tes1.txt
StringBuilder textBuilder = new StringBuilder();
String line;
String gab="";
while((line = reader.readLine()) != null) {
gab+=line;
textBuilder.append(line);
textBuilder.append("\n");
}
textView.setText(textBuilder);
Log.v("isi",baca+"="+gab);
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
String getPathSD(){
String abs="";
String state = Environment.getExternalStorageState();
if (!(state.equals(Environment.MEDIA_MOUNTED))) {
Toast.makeText(this, "There is no any sd card", Toast.LENGTH_LONG).show();
} else {
try {
File file = Environment.getExternalStorageDirectory();
abs=file.getAbsolutePath();
Toast.makeText(this, "Sd card available :"+abs, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
return abs;
}
public static boolean createDirIfNotExists(String path) {
boolean ret = true;
File file = new File(Environment.getExternalStorageDirectory(), path);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("TravellerLog :: ", "Problem creating Image folder");
ret = false;
}
}
return ret;
}
public static void copyFileOrDirectory(String srcDir, String dstDir) {
try {
File src = new File(srcDir);
File dst = new File(dstDir, src.getName());
if (src.isDirectory()) {
String files[] = src.list();
int filesLength = files.length;
for (int i = 0; i < filesLength; i++) {
String src1 = (new File(src, files[i]).getPath());
String dst1 = dst.getPath();
copyFileOrDirectory(src1, dst1);
}
} else {
copyFile(src, dst);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists())
destFile.getParentFile().mkdirs();
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}
}
Tidak ada komentar:
Posting Komentar