In the method below the compiler does not let me return the String variable in a return statement. If I change the local variable to a class field for the activity the error goes away. However I wanted to just return the String I got from the image cursor. What is the problem here? If it is not possible to do this, why is it not possible?
I would rather not use a global field variable for the class as the return variable. Was more interested in just getting the string information from the cursor and returning that string when I call the method inside of another method like inside of oncreate.
public String checkCursor(){
if(!filename.equals("234")){
String[] proj = {MediaStore.Images.Media.TITLE};
String selection = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, selection, null, MediaStore.Images.Media._ID );
if( imageCursor != null ){
if( imageCursor.moveToFirst() ){
String testString = (String) imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.IS_PRIVATE));
}
}
}
return testString;
}
Your variable
testStringis not defined in the scope where it is returned. It should be defined in the “outer scope” (the scope of the method) and not the scope of theifcondition.