Closed azhar5678 closed 5 years ago
here it is what i tried but it say unfortunately app stop when i connect pendrive when i dont connect pendriveit execute and show buttons. please help
package com.github.mjdev.libaums.usbfileman;
import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.MediaController; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import android.widget.VideoView;
import com.example.playenc.CryptoException; import com.example.playenc.playvedio; import com.github.magnusja.libaums.javafs.JavaFsFileSystemCreator; import com.github.mjdev.libaums.UsbMassStorageDevice; import com.github.mjdev.libaums.fs.FileSystem; import com.github.mjdev.libaums.fs.FileSystemFactory; import com.github.mjdev.libaums.fs.UsbFile; import com.github.mjdev.libaums.fs.UsbFileInputStream; import com.github.mjdev.libaums.fs.UsbFileOutputStream;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream;
import static com.github.mjdev.libaums.usbfileman.R.layout.activity_mainplay;
public class newjava extends Activity { private VideoView jj;
private static final String ACTION_USB_PERMISSION = "com.github.mjdev.libaums.USB_PERMISSION";
private static final String TAG = MainActivity.class.getSimpleName();
private static final int COPY_STORAGE_PROVIDER_RESULT = 0;
private static final int OPEN_STORAGE_PROVIDER_RESULT = 1;
private static final int OPEN_DOCUMENT_TREE_RESULT = 2;
private int currentDevice = -1;
private static final int REQUEST_EXT_STORAGE_WRITE_PERM = 0;
UsbMassStorageDevice[] massStorageDevices;
static {
FileSystemFactory.registerFileSystem(new JavaFsFileSystemCreator());
}
/**
* Action string to request the permission to communicate with an UsbDevice.
*/
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
// setupDevice();
}
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.d(TAG, "USB device attached");
// determine if connected device is a mass storage devuce
if (device != null) {
// discoverDevice();
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.d(TAG, "USB device detached");
// determine if connected device is a mass storage devuce
if (device != null) {
if (newjava.this.currentDevice != -1) {
newjava.this.massStorageDevices[currentDevice].close();
}
// check if there are other devices or set action bar title
// to no device if not
// discoverDevice();
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Configuration config = getResources().getConfiguration();
if (config.smallestScreenWidthDp >= 600) {
setContentView(activity_mainplay);
} else {
setContentView(activity_mainplay);
}
final Context ctx = getApplicationContext();
UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(ctx);
for (UsbMassStorageDevice device : devices) {
// before interacting with a device you need to call init()!
String actionString = ctx.getPackageName() + ".action.USB_PERMISSION";
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(ctx, 0, new
Intent(actionString), 0);
UsbManager manager = (UsbManager) newjava.this.getSystemService(ctx.USB_SERVICE);
// device.init();
// Only uses the first partition on the device
FileSystem currentFs = device.getPartitions().get(0).getFileSystem();
Toast.makeText(newjava.this, "chunk size=="+currentFs.getChunkSize(), 1000).show();
Toast.makeText(newjava.this, "occ size=="+currentFs.getOccupiedSpace(), 1000).show();
Log.d(TAG, "Capacity: " + currentFs.getCapacity());
Log.d(TAG, "Occupied Space: " + currentFs.getOccupiedSpace());
Log.d(TAG, "Free Space: " + currentFs.getFreeSpace());
Log.d(TAG, "Chunk size: " + currentFs.getChunkSize());
final MediaController mediaController= new MediaController(newjava.this);
jj = (VideoView)newjava.this.findViewById(R.id.vdVw);
// Inflate the menu; this adds items to the action bar if it is present.
final EditText ein=(EditText)findViewById(R.id.tdd);
UsbFile root = currentFs.getRootDirectory();
UsbFile[] files = new UsbFile[0];
try {
files = root.listFiles();
// getfile(files,mediaController,jj,ein);
} catch (IOException e) {
e.printStackTrace();
}
UsbFile newDir = null;
try {
newDir = root.createDirectory("foo");
} catch (IOException e) {
e.printStackTrace();
}
UsbFile file = null;
try {
file = newDir.createFile("bar.txt");
} catch (IOException e) {
e.printStackTrace();
}
// write to a file
OutputStream os = new UsbFileOutputStream(file);
try {
os.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
// read from a file
InputStream is = new UsbFileInputStream(file);
byte[] buffer = new byte[currentFs.getChunkSize()];
try {
is.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void getfile(UsbFile[] files, final MediaController mediaController, VideoView jj, final EditText ein) {
for (UsbFile file : files) {
Log.d(TAG, file.getName());
if (file.isDirectory() && file.getName().equals("0005")) {
try {
UsbFile[] ff = new UsbFile[0];
try {
ff = file.listFiles();
} catch (IOException e) {
e.printStackTrace();
}
for (UsbFile f : ff) {
if (f.isDirectory() && f.getName().equals("4533")) {
{
UsbFile[] ff1 = new UsbFile[0];
try {
ff1 = f.listFiles();
} catch (IOException e) {
e.printStackTrace();
}
for (UsbFile f1 : ff1) {
if ( f.getName().equals("123456")) {
{
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(newjava.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
horizontalScrollView.setLayoutParams(layoutParams);
LinearLayout linLayout1 = new LinearLayout(newjava.this);
ScrollView scroll = new ScrollView(newjava.this);
scroll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
LinearLayout linLayout = new LinearLayout(newjava.this);
// specifying vertical orientation
linLayout.setOrientation(LinearLayout.VERTICAL);
// creating LayoutParams
LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// set LinearLayout as a root element of the screen
//(horizontalScrollView , linLayoutParam);
//scroll.addView(linLayout);
LinearLayout.LayoutParams lpView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(newjava.this);
tv.setText("TextView");
tv.setLayoutParams(lpView);
linLayout.addView(tv);
Button btn = new Button(newjava.this);
btn.setText("Button");
linLayout.addView(btn, lpView);
String line;
InputStream is = new UsbFileInputStream(f);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
//in = new BufferedReader((Reader) (ein.getText())+"/12345.txt");
//BufferedReader br = new BufferedReader(new FileReader(fileout));
// in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename), encoding));
while ((line = in.readLine()) != null) {
if(countOccurrences(line, '\t') == 1) {
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50+1*5;
final Button btn1 = new Button(newjava.this);
btn1.setText("Button1");
btn1.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(newjava.this, btn1.getText(), 1000).show();
}
});
linLayout.addView(btn1, leftMarginParams);
}
else if (countOccurrences(line, '\t') == 2) {
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50+2*5;
final Button btn1 = new Button(newjava.this);
btn1.setText("Button1");
btn1.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(newjava.this, btn1.getText(), 1000).show();
}
});
linLayout.addView(btn1, leftMarginParams);
} else if (countOccurrences(line, '\t') == 3) {
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50+3*5;
final Button btn1 = new Button(newjava.this);
btn1.setText("Button1");
btn1.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(newjava.this, btn1.getText(), 1000).show();
}
});
linLayout.addView(btn1, leftMarginParams);
}
else if (countOccurrences(line, '\t') == 4) {
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50+4*5;
final Button btn1 = new Button(newjava.this);
btn1.setText("Button1");
btn1.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(newjava.this, btn1.getText(), 1000).show();
}
});
linLayout.addView(btn1, leftMarginParams);
}
else if (countOccurrences(line, '\t') == 5) {
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50+5*5;
final Button btn1 = new Button(newjava.this);
btn1.setText("Button1");
btn1.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(newjava.this, btn1.getText(), 1000).show();
try {
playvedio ff = new playvedio(newjava.this, newjava.this.jj,newjava.this,mediaController, ein.getText(),btn1.getText());
} catch (CryptoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
linLayout.addView(btn1, leftMarginParams);
}
else if (countOccurrences(line, '\t') == 6) {
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
leftMarginParams.leftMargin = 50+6*5;
final Button btn1 = new Button(newjava.this);
btn1.setText("Button1");
btn1.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(newjava.this, btn1.getText(), 1000).show();
}
});
linLayout.addView(btn1, leftMarginParams);
LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
rightGravityParams.gravity = Gravity.RIGHT;
Button btn2 = new Button(newjava.this);
btn2.setText("Button2");
linLayout.addView(btn2, rightGravityParams);
//linLayout1.addView(linLayout);
horizontalScrollView .addView(scroll);
scroll.addView(linLayout);
}
//continue the else...if - if you have more levels
}
in.close();
}
}
}
}
}
}
getfile(ff, mediaController, jj, ein);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private int countOccurrences(String haystack, char needle) {
int count = 0;
for (int i = 0; i < haystack.length(); i++) {
if (haystack.charAt(i) == needle) {
count++;
}
}
return count;
}
}
Does the exmaple application work for you?
What is the stacktrace of the exception?
Example application works fine. but that contains inner classes and It's hard to predict what's going on . when all code is in single page.
may permission needed all the time of accessing USB. But I am unable to understand what's going on.
In example I don't need copy task. Run tests. On Jul 4, 2019 4:08 PM, "Magnus" notifications@github.com wrote:
Does the exmaple application work for you?
What is the stacktrace of the exception?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/magnusja/libaums/issues/191?email_source=notifications&email_token=AFSPX4YXV4UEXEGZ5PM5NS3P5XHMBA5CNFSM4H5B2G6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZHBUMI#issuecomment-508434993, or mute the thread https://github.com/notifications/unsubscribe-auth/AFSPX43VUAIZDELSHCCDM3TP5XHMBANCNFSM4H5B2G6A .
Well then just compare to the example app, that what it is for.
The permission is extensively explained in the Android developer documentation.
And at least post some stack traces. I will not copy your code from here and try to get this work...
I will talk to u later. thanks. For the reply. On Jul 4, 2019 4:33 PM, "Magnus" notifications@github.com wrote:
Well then just compare to the example app, that what it is for.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/magnusja/libaums/issues/191?email_source=notifications&email_token=AFSPX4YUMZXO2JPPNJUWYK3P5XKHFA5CNFSM4H5B2G6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZHDJ7A#issuecomment-508441852, or mute the thread https://github.com/notifications/unsubscribe-auth/AFSPX47DN7223SQ5DPU6DE3P5XKHFANCNFSM4H5B2G6A .
error on using toast.maketext for currentFs.getChunkSize()
errorr recieving bradcast intent act=com.github...........................usb_permission
Can you post the full thing?
i am working on android 6 , what version of libaums do we need to use in it. do we need to create stub IUsbManager.java as mentioned in https://stackoverflow.com/questions/13726583/bypass-android-usb-host-permission-confirmation-dialog
UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(this / Context or Activity /);
for(UsbMassStorageDevice device: devices) {
}
app crashes with this code am using it in on create method of activity
and how do i get permisson to read and what is broadcast reciever doing on start
please give me simple code to list files with all the permission
because ur example activity contains lots of other code which i dont need