Android 5 SD card label - android

When I send ACTION_OPEN_DOCUMENT_TREE intent I can see some kind of SD card label.
How I can get it in my application?

You should send this intent using startActivityForResult method.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, REQUEST_CODE);
After that you can get the uuid in your onActivityResult method.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri treeUri = intent.getData();
final int splitIndex = treeId.indexOf(':', 1);
final String uuid = treeId.substring(0, splitIndex);
}
uuid is the text, that your are looking for.
If you need to get uuid of all storages, without showing documents tree picker, you can use StorageManager. But getVolumeList, and getUuid methods are hidden so I used reflection to get it.
StorageManager mStorageManager = (android.os.storage.StorageManager) mContext
.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getUuid = storageVolumeClazz.getMethod("getUuid");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String uuid = (String) getUuid.invoke(storageVolumeElement);
}

Related

how sent integer parameter in intent

I want to send int parameter in intent like this:
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
and in main class I get that parameter
Intent intent = getIntent();
name = intent.getStringExtra("name");
page1 = Integer.parseInt(intent.getStringExtra("page"));
but when I run my code it force closes!!!
You should use getIntent().getIntExtra(name, defaultValue) instead of Integer.parseInt(intent.getStringExtra("page"));
Update:
int defaultValue = -1;// take any default value of your choice
String name = intent.getStringExtra("name");
int page1 = intent.getIntExtra("page", defaultValue);
I in other class use the main class and send parameter to it and it work without any problem but just in list class i have problem
in other class i like this send parameter
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(list_sub.this,main.class);
i.putExtra("name", Name[position]);
i.putExtra("page", Ted[position]);
startActivity(i);
}
and in refresh class i get ted parameter
private void refresh(){
db.open();
int s = db.List_count("text", ID);
Name= new String[s];
Ted=new String[s];
for(int i=0;i<s;i++){
Name[i]=db.List_display_sub("text", ID, i);
Ted[i]=db.page_count("text", Name[i].toString())+"";
}
db.close();
}
Activity A
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
Activity B
Intent intent = getIntent();
name = intent.getStringExtra("name");
int page = intent.getIntExtra("page", 0);
where 0 is the default value.

Call onActivityResult for contact in OnCreate() Android

I got this code from another question but I don't know how to call this onActivityResult() class in my onCreate() activity to display the first contact from my phone. Also, what does "if (requestCode == RQS_PICKCONTACT){" and "RQS_PICKCONTACT" stand for? Could someone please clarify?
public class MainActivity extends Activity {
Button buttonReadContact;
TextView textPhone;
final int RQS_PICKCONTACT = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonReadContact = (Button)findViewById(R.id.readcontact);
textPhone = (TextView)findViewById(R.id.phone);
buttonReadContact.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//Start activity to get contact
/*final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
startActivityForResult(intentPickContact, RQS_PICKCONTACT);
*/
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, RQS_PICKCONTACT);
}});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == RESULT_OK) {
if(requestCode == RQS_PICKCONTACT) {
Uri returnUri = data.getData();
Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
if (cursor.moveToNext()) {
int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = cursor.getString(columnIndex_ID);
int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
if(stringHasPhoneNumber.equalsIgnoreCase("1")){
Cursor cursorNum = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID,
null,
null);
//Get the first phone number
if(cursorNum.moveToNext()){
int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String stringNumber = cursorNum.getString(columnIndex_number);
textPhone.setText("0"+stringNumber);
}
} else {
textPhone.setText("NO Phone Number");
}
} else {
Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
}
}
}
}
When you call startActivityForResult(intent,requestCode)
onActivityResult is called when user comes back to calling activity with
requestCode
//You can start multiple activities by calling startActivityForResult so this value is to differentiate between them
resultCode
//This value is set by the called activity to indicate whether the intended operation was a success or not.
data
//this is an object of type Intent which contains data returned by called activity.
in your code when this part is executed:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, RQS_PICKCONTACT);
New activity is started and when user comes back from that activity by selecting a contact onActivityResult is called
onActivityResult is called after u startIntent or u select an contact.
RQS_PICK_CONTACT u can change as u want. like 2 , 3,4 or another number.
it just identity for requestCode in onActivityResult so u can do as u need.

Encode in QRCode using ZXING

I'm trying to encode a String in QR Code wusing ZXING library. this is the lines of code corresponding to this :
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
intent.putExtra("ENCODE_DATA","HELLO WORLD");
startActivityForResult(intent, 0);
}
});
}
After clicking on the button i have a "force close"
After looking in some websites, we say that it works just with these lines. unfortunately, it isnt for me.
PLEASE Can you give some advices to make it working. OR if you have other way to integrate a QRCode generator to my ANDROID App it will be great too.
Enzo, this is another way to get it working, try this:
private void encode(String uniqueID) {
// TODO Auto-generated method stub
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
int width0 = 500;
int height0 = 500;
int colorBack = 0xFF000000;
int colorFront = 0xFFFFFFFF;
QRCodeWriter writer = new QRCodeWriter();
try
{
EnumMap<EncodeHintType, Object> hint = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
hint.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = writer.encode(uniqueID, barcodeFormat, width0, height0, hint);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++)
{
int offset = y * width;
for (int x = 0; x < width; x++)
{
pixels[offset + x] = bitMatrix.get(x, y) ? colorBack : colorFront;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
ImageView imageview = (ImageView)findViewById(R.id.qrCode);
imageview.setImageBitmap(bitmap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This line:
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
identifies the Activity you are calling with the Intent. In order for it to work that Activity must exist on the device. If you haven't implemented the Activity inside your project (that is, there is no class ENCODE inside your com.google.zxing.client.android package) then you will be calling an external application from yours. If there are no applications on the device/emulator that respond to the broadcast for com.google.zxing.client.android.ENCODE then you are not going to get anywhere with this solution.
You either need to install an application that will respond to com.google.zxing.client.android.ENCODE or find another way to do it.
It is possible to generate the barcode yourself using zxing libraries within your application. Have a look at the project here at Google Code for some downloads. This will remove your dependancy on an external application existing, providing a more thorough solution.
Rather than Zxing library you are also able to get the string from QRCode by using intents as below :
try {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes
startActivityForResult(intent, 0);
} catch (Exception e) {
Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
startActivity(marketIntent);
}
And on activity result as below :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
Log.v("MESSAGE: ", contents);
Intent in = new Intent(MainActivity2.this,MainActivity3.class);
Bundle b3= new Bundle();
b3.putString("content",contents);
in.putExtras(b3);
startActivity(in);
}
}
}
It may be helpful for you.
Thank You.

Android pick intent

I am trying to pick a file with any of these 3 mime types, and it doesn't seem to work
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*, video/*, audio/*");
Can someone suggest how I can do it ?
Write below code instead of your code, it may help you.
private static final int PICTURE = 0;
private static final int VIDEO = 1;
private static final int AUDIO = 2;
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
String title = GET_PICTURE;
if (this.mediaType == PICTURE) {
photoPickerIntent.setType("image/*");
title = "GET_PICTURE";
}else if (this.mediaType == VIDEO) {
photoPickerIntent.setType("video/*");
title = "GET_VIDEO";
}else if (this.mediaType == AUDIO) {
photoPickerIntent.setType("audio/*");
title = "GET_AUDIO";
}
And Use below links for reference.
Pick Intent Example

Android Intent - How can i open file

To open image or sound we can use this code:
public String getExt(String filename)
{
int dotIndex = 0;
for(int i = filename.length()-1; i >= 0; i--)
{
if(filename.charAt(i) == '.')
{
dotIndex = i;
break;
}
}
return filename.substring(dotIndex + 1, filename.length());
}
public String getTypeAction(String s)
{
int slashIndex = 0;
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == '/')
{
slashIndex = i;
break;
}
}
return s.substring(0, slashIndex);
}
//////
//////
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = getExt(file_to_action.getName());
ext = ext.toLowerCase();
String type = mime.getMimeTypeFromExtension(ext);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file_to_action), getTypeAction(type) + "/*" );
startActivity(intent); !>
But the problem is when i want to open any different file for example: .mhtml, .bak. apk. When i do this my application is crash. I don't know where is the problem, i have application for .mhtml, .odt extenio but my application can't use this applications to open the file.
How i can open all file format or how i can check avaliable application for any file format ?
you can also do this
File file = new File(filepath);
String extension = file.getName().substring(file.getName().lastIndexOf(".")+1);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension(extension);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), type);
startActivity(intent);
This crashes because there is no application to handle that type of data, installed on your phone.
You should use queryIntentActivies(intent,flags) from the PackageManager to see if there is any application that can handle your Intent. If the List<ResolveInfo> size is greater than 0, than there is some application that can handle your data. If the size is 0, you should display a Toast and inform the user that the data can not be handled, and also do not call startActivity() in this case.

Categories

Resources